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);
    }

    @Override
    public boolean onCreate() {
    
    
        mHelper = new MyDatabaseHelper(getContext(), "BookStore.db", null, 4);
        return true;
    }

    @Nullable
    @Override
    public 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
    @Override
    public 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
    @Override
    public 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;
    }

    @Override
    public 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;
    }

    @Override
    public 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()) {
    
    
                // 遍历Cursor对象,取出数据并打印
                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

Guess you like

Origin blog.csdn.net/weixin_44495678/article/details/121598179