4.6

所花时间(包括上课):1

打码量(行):200

博客量(篇):1

了解到知识点:学习SimpleProvider

 

import android.content.ContentProvider;

import android.content.ContentUris;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.content.UriMatcher;

import android.database.MatrixCursor;

import android.support.annotation.NonNull;

import android.support.annotation.Nullable;

import java.util.Arrays;

import java.util.HashSet;

import java.util.Set;

 

public class MyContentProvider extends ContentProvider {

 

    // 数据库名称

    private static final String DB_NAME = "my_database";

 

    // 表名

    private static final String TABLE_NAME = "my_table";

 

    // Content Provider 的 authority

    private static final String AUTHORITY = "com.example.mycontentprovider";

 

    // Content Provider 中的 Uri

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);

 

    // UriMatcher

    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

 

    // Uri 匹配码

    private static final int ITEMS = 1;

    private static final int ITEMS_ID = 2;

 

    // 初始化 UriMatcher

    static {

        sUriMatcher.addURI(AUTHORITY, TABLE_NAME, ITEMS);

        sUriMatcher.addURI(AUTHORITY, TABLE_NAME + "/#", ITEMS_ID);

    }

 

    // 数据库帮助类

    private MyDatabaseHelper dbHelper;

 

    @Override

    public boolean onCreate() {

        dbHelper = new MyDatabaseHelper(getContext(), DB_NAME, null, 1);

        return true;

    }

 

    @Nullable

    @Override

    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,

                        @Nullable String[] selectionArgs, @Nullable String sortOrder) {

        switch (sUriMatcher.match(uri)) {

            case ITEMS:

                // 查询全部数据

                return dbHelper.getReadableDatabase().query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);

            case ITEMS_ID:

                // 查询单条数据

                long id = ContentUris.parseId(uri);

                return dbHelper.getReadableDatabase().query(TABLE_NAME, projection, "_id=?", new String[]{String.valueOf(id)}, null, null, sortOrder);

            default:

                throw new IllegalArgumentException("Unknown URI: " + uri);

        }

    }

 

    @Nullable

    @Override

    public String getType(@NonNull Uri uri) {

        switch (sUriMatcher.match(uri)) {

            case ITEMS:

                return "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + TABLE_NAME;

            case ITEMS_ID:

                return "vnd.android.cursor.item/vnd." + AUTHORITY + "." + TABLE_NAME;

            default:

                throw new IllegalArgumentException("Unsupported URI: " + uri);

        }

    }

 

    @Nullable

    @Override

    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

        switch (sUriMatcher.match(uri)) {

            case ITEMS:

                long id = dbHelper.getWritableDatabase().insert(TABLE_NAME, null, values);

                if (id > 0) {

                    Uri itemUri = ContentUris.withAppendedId(CONTENT_URI, id);

                    getContext().getContentResolver().notifyChange(itemUri, null);

                    return itemUri;

                }

                throw new IllegalArgumentException("Failed to insert row into " + uri);

            default:

                throw new IllegalArgumentException("Unsupported URI: " + uri);

        }

    }

 

    @Override

    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {

        switch (sUriMatcher.match(uri)) {

            case ITEMS:

                return dbHelper.getWritableDatabase().delete(TABLE_NAME, selection, selectionArgs);

            case ITEMS_ID:

                long id = ContentUris.parseId(uri);

                return dbHelper.getWritableDatabase().delete(TABLE_NAME, "_id=?", new String[]{String.valueOf(id)});

            default:

                throw new IllegalArgumentException("Unsupported URI: " + uri);

        }

    }

 

    @Override

    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,

                      @Nullable String[] selectionArgs) {

        switch (sUriMatcher.match(uri)) {

            case ITEMS:

                return dbHelper.getWritableDatabase().update(TABLE_NAME, values, selection, selectionArgs);

            case ITEMS_ID:

                long id = ContentUris.parseId(uri);

                return dbHelper.getWritableDatabase().update(TABLE_NAME, values, "_id=?", new String[]{String.valueOf(id)});

            default:

                throw new IllegalArgumentException("Unsupported URI: " + uri);

        }

    }

 

    // 自定义的数据库帮助类

    private static class MyDatabaseHelper extends SQLiteOpenHelper {

 

        MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {

            super(context, name, factory, version);

        }

 

        @Override

        public void onCreate(SQLiteDatabase db) {

            db.execSQL("CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");

        }

 

        @Override

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

            onCreate(db);

        }

    }

}

posted @ 2024-04-06 20:59  赵千万  阅读(6)  评论(0编辑  收藏  举报