标签:
public class PersonContentProvider extends ContentProvider{ private DataBaseOpenHelper dataBaseOpenHelper; private static final int ALLPERSON=1; private static final int PERSON=2; private static final UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH); static{ uriMatcher.addURI("com.gao.provider.personprovider", "person", ALLPERSON); uriMatcher.addURI("com.gao.provider.personprovider", "person/#", PERSON); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase database=dataBaseOpenHelper.getWritableDatabase(); int count=0; switch (uriMatcher.match(uri)) { case ALLPERSON://content://com.gao.provider.personprovider/person count=database.delete("person", selection, selectionArgs); break; case PERSON://content://com.gao.provider.personprovider/person/100 long id=ContentUris.parseId(uri); String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?"; String[] params=new String[]{String.valueOf(id)}; if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) { params=new String[selectionArgs.length+1]; for (int i = 0; i < selectionArgs.length; i++) { params[i]=selectionArgs[i]; } params[selectionArgs.length+1]=String.valueOf(id); } count=database.delete("person", where, selectionArgs); break; default: throw new IllegalArgumentException("Unkonw uri:"+uri); } return count; } @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)) { case ALLPERSON: return "vnd.android.cursor.dir/personprovider.person"; case PERSON: return "vnd.android.cursor.item/personprovider.person"; default: break; } return null; } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase database=dataBaseOpenHelper.getWritableDatabase(); long id=0; switch (uriMatcher.match(uri)) { case ALLPERSON://content://com.gao.provider.personprovider/person id=database.insert("person", "name", values);//返回记录的行号,主键是int,实际上就是主键值 return ContentUris.withAppendedId(uri, id); case PERSON://content://com.gao.provider.personprovider/person/100 id=database.insert("person", "name", values); String path=uri.toString(); return Uri.parse(path.substring(0, path.lastIndexOf("/"))+"id"); default: throw new IllegalArgumentException("Unkonw uri:"+uri); } } @Override public boolean onCreate() { dataBaseOpenHelper=new DataBaseOpenHelper(this.getContext()); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase database=dataBaseOpenHelper.getReadableDatabase(); switch (uriMatcher.match(uri)) { case ALLPERSON: database.query("person", projection, selection, selectionArgs, null, null, sortOrder); break; case PERSON: long id=ContentUris.parseId(uri); String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?"; String[] params=new String[]{String.valueOf(id)}; if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) { params=new String[selectionArgs.length+1]; for (int i = 0; i < selectionArgs.length; i++) { params[i]=selectionArgs[i]; } params[selectionArgs.length+1]=String.valueOf(id); } database.query("person", projection, where, params, null, null, sortOrder); break; default: break; } return null; } //update("content://com.gao.provider.personprovider/person/100",values,"name like ? and ...",new String[]{"%gao%"}); @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase database=dataBaseOpenHelper.getWritableDatabase(); int count=0; switch (uriMatcher.match(uri)) { case ALLPERSON://content://com.gao.provider.personprovider/person count=database.update("person", values, selection,selectionArgs); break; case PERSON://content://com.gao.provider.personprovider/person/100 long id=ContentUris.parseId(uri); String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?"; String[] params=new String[]{String.valueOf(id)}; if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) { params=new String[selectionArgs.length+1]; for (int i = 0; i < selectionArgs.length; i++) { params[i]=selectionArgs[i]; } params[selectionArgs.length+1]=String.valueOf(id); } count=database.update("person", values, where,params); break; default: throw new IllegalArgumentException("Unkonw uri:"+uri); } return count; } }
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ContentResolver contentResolver=this.getContentResolver(); Uri allUri=Uri.parse("content://com.gao.provider.personprovider/person"); ContentValues values=new ContentValues(); values.put("name", "GGGGG"); values.put("age", 19); contentResolver.insert(uri, values); values.put("name", "CCCCCC"); values.put("age", 100); Uri uri=Uri.parse("content://com.gao.provider.personprovider/person/9"); // contentResolver.update(uri, values, null, null); //contentResolver.delete(uri, null, null); Cursor cursor=contentResolver.query(uri, new String[]{"personid","name","age"}, null, null, "personid desc"); while (cursor.moveToNext()) { Log.i(TAG, "psrsonid:"+cursor.getInt(0)+",name:"+cursor.getString(1)+",age"+cursor.getInt(2)); } cursor.close(); }
ContentProvider与ContentResolver
标签:
原文地址:http://www.cnblogs.com/zhujiabin/p/4206423.html