生活随笔
收集整理的這篇文章主要介紹了
Android【ContentProvider案例】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ContentProvider實現跨程序數據共享,實例如下:
數據庫基于數據存儲中的使用的數據庫,點擊Here
1】ContentProvider類
public class ContentProvider extends android.content.ContentProvider {private static final int BOOK_DIR
= 0;private static final int BOOK_ITEM
= 1;private static final int CATEGORY_DIR
= 2;private static final int CATEGORY_ITEM
= 3;private static final String AUTHORITY
= "com.example.android.provider";private static UriMatcher mUriMatcher
;private MyDatabaseHelper mHelper
;static {mUriMatcher
= new UriMatcher(UriMatcher.NO_MATCH
);mUriMatcher
.addURI(AUTHORITY
, "book", BOOK_DIR
);mUriMatcher
.addURI(AUTHORITY
, "book/#", BOOK_ITEM
);mUriMatcher
.addURI(AUTHORITY
, "category", CATEGORY_DIR
);mUriMatcher
.addURI(AUTHORITY
, "category/#", CATEGORY_ITEM
);}@Overridepublic boolean onCreate() {mHelper
= new MyDatabaseHelper(getContext(), "BookStore.db", null, 4);return true;}@Nullable@Overridepublic Cursor query(@NonNull Uri uri
, @Nullable String[] projection
, @Nullable String selection
, @Nullable String[] selectionArgs
, @Nullable String sortOrder
) {SQLiteDatabase db
= mHelper
.getWritableDatabase();Cursor cursor
= null;switch (mUriMatcher
.match(uri
)) {case BOOK_DIR
:cursor
= db
.query("Book", projection
, selection
, selectionArgs
, null, null, sortOrder
);break;case BOOK_ITEM
:String bookId
= uri
.getPathSegments().get(1);cursor
= db
.query("Book", projection
, "id=?", new String[]{bookId
}, null, null, sortOrder
);break;case CATEGORY_DIR
:cursor
= db
.query("Category", projection
, selection
, selectionArgs
, null, null, sortOrder
);break;case CATEGORY_ITEM
:String categoryId
= uri
.getPathSegments().get(1);cursor
= db
.query("Category", projection
, "id=?", new String[]{categoryId
}, null, null, sortOrder
);break;default:break;}return cursor
;}@Nullable@Overridepublic String getType(@NonNull Uri uri
) {switch (mUriMatcher
.match(uri
)) {case BOOK_DIR
:return "vnd.android.cursor.dir/vnd.com.example.android.provider.book";case BOOK_ITEM
:return "vnd.android.cursor.item/vnd.com.example.android.provider.book";case CATEGORY_DIR
:return "vnd.android.cursor.dir/vnd.com.example.android.provider.category";case CATEGORY_ITEM
:return "vnd.android.cursor.item/vnd.com.example.android.provider.category";default:break;}return null;}@Nullable@Overridepublic Uri insert(@NonNull Uri uri
, @Nullable ContentValues values
) {SQLiteDatabase db
= mHelper
.getWritableDatabase();Uri uriRe
= null;switch (mUriMatcher
.match(uri
)) {case BOOK_DIR
:case BOOK_ITEM
:long newBookId
= db
.insert("Book", null, values
);uriRe
= Uri.parse("context://" + AUTHORITY
+ "/book" + newBookId
);break;case CATEGORY_DIR
:case CATEGORY_ITEM
:long newCategoryId
= db
.insert("Category", null, values
);uriRe
= Uri.parse("context://" + AUTHORITY
+ "/category" + newCategoryId
);break;default:break;}return uriRe
;}@Overridepublic int delete(@NonNull Uri uri
, @Nullable String selection
, @Nullable String[] selectionArgs
) {SQLiteDatabase db
= mHelper
.getWritableDatabase();int deleteRows
= 0;switch (mUriMatcher
.match(uri
)) {case BOOK_DIR
:deleteRows
= db
.delete("Book", selection
, selectionArgs
);break;case BOOK_ITEM
:String bookId
= uri
.getPathSegments().get(1);deleteRows
= db
.delete("Book", "id=?", new String[]{bookId
});break;case CATEGORY_DIR
:deleteRows
= db
.delete("Category", selection
, selectionArgs
);break;case CATEGORY_ITEM
:String categoryId
= uri
.getPathSegments().get(1);deleteRows
= db
.delete("Category", "id=?", new String[]{categoryId
});break;}return deleteRows
;}@Overridepublic int update(@NonNull Uri uri
, @Nullable ContentValues values
, @Nullable String selection
, @Nullable String[] selectionArgs
) {SQLiteDatabase db
= mHelper
.getWritableDatabase();int updateRows
= 0;switch (mUriMatcher
.match(uri
)) {case BOOK_DIR
:updateRows
= db
.update("Book", values
, selection
, selectionArgs
);break;case BOOK_ITEM
:String bookId
= uri
.getPathSegments().get(1);updateRows
= db
.update("Book", values
, "id=?", new String[]{bookId
});break;case CATEGORY_DIR
:updateRows
= db
.update("Category", values
, selection
, selectionArgs
);break;case CATEGORY_ITEM
:String categoryId
= uri
.getPathSegments().get(1);updateRows
= db
.update("Category", values
, "id=?", new String[]{categoryId
});break;}return updateRows
;}
}
2】實現增刪改查
查詢數據
private void queryData() {Uri uri
= Uri.parse(AGREE
+ AUTHORITY
+ CATEGORY
);Cursor cursor
= getContentResolver().query(uri
, null, null, null, null, null);if (cursor
!= null) {while (cursor
.moveToNext()) {String category_name
= cursor
.getString(cursor
.getColumnIndex("category_name"));String category_code
= cursor
.getString(cursor
.getColumnIndex("category_code"));Log.d(TAG
, "book name is " + category_name
);Log.d(TAG
, "book author is " + category_code
);}}cursor
.close();}
更新數據
private void updateData() {Uri uri
= Uri.parse(AGREE
+ AUTHORITY
+ CATEGORY
);ContentValues values
= new ContentValues();values
.put("category_name", "科幻類");values
.put("category_code", 66);getContentResolver().update(uri
, values
, "category_name=?", new String[]{"科幻類"});}
刪除數據
private void deleteData() {Uri uri
= Uri.parse(AGREE
+ AUTHORITY
+ CATEGORY
+ "/" + newId
);getContentResolver().delete(uri
, null, null);}
添加數據
private void InsertData() {Uri uri
= Uri.parse(AGREE
+ AUTHORITY
+ CATEGORY
);ContentValues values
= new ContentValues();values
.put("category_name", "科幻類");values
.put("category_code", 11);Uri newUri
= getContentResolver().insert(uri
, values
);newId
= newUri
.getPathSegments().get(1);}
運行結果,查詢數據pass,其他操作failed
總結
以上是生活随笔為你收集整理的Android【ContentProvider案例】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。