android开发第六天ContentProvider数据共享

provider

public class provider extends ContentProvider {
    public final String TAG="provider";
    /**
     * 这个要和manifest.xml中的一致
     */
    public static final String AUTHORITY = "com.nanguabing.contentprovider.myprovider";
    private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        try {
            matcher.addURI(AUTHORITY, "insert", 1);
            matcher.addURI(AUTHORITY, "delete/#", 2);
            matcher.addURI(AUTHORITY, "update/#", 3);
            matcher.addURI(AUTHORITY, "query/#", 4);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    CRUD mCRUD;

    @Override
    public boolean onCreate() {
        mCRUD = new CRUD(this.getContext());
        return false;
    }

    /**
     * 查询操作
     */
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        int code = matcher.match(uri);
        if (code == 4) {
        return mCRUD.findAll();
        }
        return null;
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 插入操作
     */
    public Uri insert(Uri uri, ContentValues values) {
        // uri=content://com.nanguabing.contentprovider.myprovider/insert
        int code = matcher.match(uri);
        long id = -1;
        if (code == 1) {
            mCRUD.insert(values.getAsString("name"),values.getAsInteger("age"));
            id=1;
        } else {
            Log.i(TAG, "this uri is invalid for insert!");
        }
        return ContentUris.withAppendedId(uri, id);
    }

    /**
     * 删除操作
     */
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        //delete:uri=content://cn.itcast.provider.customerprovider/delete/10
        int code = matcher.match(uri);
        if(code == 2){
            mCRUD.delete((int) ContentUris.parseId(uri));
        }
        else{
            Log.i(TAG, "this uri is invalid for delete!");
        }
        return 0;
    }

    /**
     * 修改操作
     */
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        //update:uri=content://cn.itcast.provider.customerprovider/update/10
        int code = matcher.match(uri);
        if(code == 3){
            mCRUD.update(values.getAsString("name"),values.getAsInteger("age"),values.getAsInteger("id"));
        }
        else{
            Log.i(TAG, "this uri is invalid for update!");
        }
        return 0;
    }

}

mainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nanguabing.contentprovider"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.nanguabing.contentprovider.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:name="com.nanguabing.contentprovider.provider"
                android:authorities="com.nanguabing.contentprovider.myprovider"/>
    </application>

</manifest>

activity

public class MainActivity extends Activity {
    String uri = "";
    TextView mTV;
    EditText mID, mName, mAge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTV = (TextView) findViewById(R.id.textView1);
        mID = (EditText) findViewById(R.id.editText1);
        mName = (EditText) findViewById(R.id.editText2);
        mAge = (EditText) findViewById(R.id.editText3);
    }

    public void onClick(View v) {
        ContentResolver cr = this.getContentResolver();
        ContentValues values = new ContentValues();
        switch (v.getId()) {
        case R.id.button1:
            uri = "content://com.nanguabing.contentprovider.myprovider/insert";
            values.put("name", mName.getText().toString());
            values.put("age", Integer.parseInt(mAge.getText().toString()));
            cr.insert(Uri.parse(uri), values);
            break;
        case R.id.button2:
            uri = "content://com.nanguabing.contentprovider.myprovider/delete/"
                    + mID.getText().toString();
            cr.delete(Uri.parse(uri), null, null);
            break;
        case R.id.button3:
            uri = "content://com.nanguabing.contentprovider.myprovider/update/"
                    + mID.getText().toString();
            values.put("id", Integer.parseInt(mID.getText().toString()));
            values.put("name", mName.getText().toString());
            values.put("age", Integer.parseInt(mAge.getText().toString()));
            cr.update(Uri.parse(uri), values, null, null);
            break;
        case R.id.button4:
            uri = "content://com.nanguabing.contentprovider.myprovider/query/1";
            Cursor cur = cr.query(Uri.parse(uri), null, null, null, null);
            String txt = "";
            while (cur.moveToNext()) {
                txt += cur.getInt(cur.getColumnIndex("id"))
                        + cur.getString(cur.getColumnIndex("name"))
                        + cur.getInt(cur.getColumnIndex("age"))+"\n";
            }
            mTV.setText(txt);
            // 关闭游标
            cur.close();
            break;
        default:
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

移动开发qq群:59516399

csdn下载链接:http://download.csdn.net/detail/wenwei19861106/4979777

posted on 2013-01-10 08:25  南瓜饼  阅读(308)  评论(0编辑  收藏  举报

导航