我的android 第21天 - 使用ContentProvider共享数据
????????package cn.itcast.db;import cn.itcast.service.DBOpenHelper;import android.content.ContentProvider;import android.content.ContentUris;import android.content.ContentValues;import android.content.UriMatcher;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.net.Uri;public class PersonContentProvider extends ContentProvider {private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);private static final int PERSONS = 1;private static final int PERSON = 2;private DBOpenHelper dbOpenHelper;static{matcher.addURI("cn.itcast.providers.personprovider", "person", PERSONS);matcher.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);}@Overridepublic boolean onCreate() {dbOpenHelper = new DBOpenHelper(this.getContext());return true;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {SQLiteDatabase db = dbOpenHelper.getWritableDatabase();int num = 0 ;//已经删除的记录数量switch (matcher.match(uri)) {case PERSONS:num = db.delete("person", selection, selectionArgs);break;case PERSON:long id = ContentUris.parseId(uri);String where = "personid="+ id;if(selection!=null && !"".equals(selection)){ // personid=12 and name=?where = where + " and "+ selection;}num = db.delete("person", where, selectionArgs);break;default:throw new IllegalArgumentException("Unkown Uri:"+ uri);}getContext().getContentResolver().notifyChange(uri, null);return num;}@Overridepublic String getType(Uri uri) {//返回当前操作的数据类型switch (matcher.match(uri)) {case PERSONS://操作的是集合类型数据return "vnd.android.cursor.dir/person";case PERSON:return "vnd.android.cursor.item/person";default:throw new IllegalArgumentException("Unkown Uri:"+ uri);}}@Overridepublic Uri insert(Uri uri, ContentValues values) {SQLiteDatabase db = dbOpenHelper.getWritableDatabase();long id = 0 ;switch (matcher.match(uri)) {case PERSONS:id = db.insert("person", "personid", values);//得到记录的idgetContext().getContentResolver().notifyChange(uri, null);return ContentUris.withAppendedId(uri, id);//返回代表新增记录的Uricase PERSON:id = db.insert("person", "personid", values);//得到记录的idString strUri = uri.toString();Uri personUri = Uri.parse(strUri.substring(0, strUri.lastIndexOf("/")));getContext().getContentResolver().notifyChange(personUri, null);return ContentUris.withAppendedId(personUri, id);default:throw new IllegalArgumentException("Unkown Uri:"+ uri);}}@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {SQLiteDatabase db = dbOpenHelper.getReadableDatabase();switch (matcher.match(uri)) {case PERSONS:return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);case PERSON:long id = ContentUris.parseId(uri);String where = "personid="+ id;if(selection!=null && !"".equals(selection)){ // personid=12 and name=?where = where + " and "+ selection;}return db.query("person", projection, where, selectionArgs, null, null, sortOrder);default:throw new IllegalArgumentException("Unkown Uri:"+ uri);}}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {SQLiteDatabase db = dbOpenHelper.getWritableDatabase();int num = 0 ;//已经修改的记录数量switch (matcher.match(uri)) {case PERSONS:num = db.update("person", values, selection, selectionArgs);break;case PERSON:long id = ContentUris.parseId(uri);String where = "personid="+ id;if(selection!=null && !"".equals(selection)){ where = where + " and "+ selection;}num = db.update("person", values, where, selectionArgs);break;default:throw new IllegalArgumentException("Unkown Uri:"+ uri);}getContext().getContentResolver().notifyChange(uri, null);//通知数据发生变化return num;}}
?
?
?
下载视频代码