ContentProvider使用完整示例(二)――ContentObserver(四)

2014-11-24 12:57:11 · 作者: · 浏览: 3
static final int PERSONS = 0; private static final int PERSON = 1; @Override public boolean onCreate() { initUriMatcher(); dbOpenHelper=new DBOpenHelper(getContext()); return true; } //初始化UriMatcher private void initUriMatcher(){ URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH); //表示返回所有的person,其中PERSONS为该特定Uri的标识码 URI_MATCHER.addURI("cn.bs.testcontentprovider","person", PERSONS); //表示返回某一个person,其中PERSON为该特定Uri的标识码 URI_MATCHER.addURI("cn.bs.testcontentprovider","person/#", PERSON); } /** * 插入操作: * 插入操作只有一种可能:向一张表中插入 * 返回结果为新增记录对应的Uri * 方法db.insert()返回结果为新增记录对应的主键值 */ @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); switch (URI_MATCHER.match(uri)) { case PERSONS: long rowid=db.insert("person", "name,phone,salary", values); //向外界通知该ContentProvider里的数据发生了变化 getContext().getContentResolver().notifyChange(uri, null); return ContentUris.withAppendedId(uri, rowid); default: throw new IllegalArgumentException("unknown uri"+uri.toString()); } } /** * 更新操作: * 更新操作有两种可能:更新一张表或者更新某条数据 * 在更新某条数据时原理类似于查询某条数据,见下. */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); int updataNum=0; switch (URI_MATCHER.match(uri)) { //更新表 case PERSONS: updataNum=db.update("person", values, selection, selectionArgs); break; //按照id更新某条数据 case PERSON: long id=ContentUris.parseId(uri); String
where="personid="+id; if(selection!=null&&!"".equals(selection.trim())){ where=selection+" and "+where; } updataNum=db.update("person", values, where, selectionArgs); break; default: throw new IllegalArgumentException("unknown uri"+uri.toString()); } //向外界通知该ContentProvider里的数据发生了变化 getContext().getContentResolver().notifyChange(uri, null); return updataNum; } //在此不实现该方法 @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db=dbOpenHelper.getWritableDatabase(); Cursor cursor; switch (URI_MATCHER.match(uri)) { //查询表 case PERSONS: cursor=db.query("person", projection, selection, selectionArgs, null, null, sortOrder); break; //按照id查询某条数据 case PERSON: //第一步: long id=ContentUris.parseId(uri); String where="personid="+id; //第二步: if(selection!=null&&!"".equals(selection.trim())){ where=selection+" and "+where; } cursor=db.query("person", projection, where, selectionArgs, null, null, sortOrder); break; default: throw new IllegalArgumentException("unknown uri"+uri.toString()); } return cursor; } /** * 返回当前Uri所代表的数据的MIME类型. * 如果该Uri对应的数据可能包含多条记录,那么返回 * 字符串应该以"vnd.android.cursor.dir/"开头 * 如果该Uri对应的数据只包含一条记录,那么返回 * 字符串应该以"vnd.android.cursor.item/"开头 */ @Override public String getType(Uri uri) { switch (URI_MATCHER.match(uri)) { case PERSONS: return "vnd.android.cursor.dir/persons"; case PERSON: return "vnd.android.cursor