自定义一个ContentProvider,重写增、删、改、查的方法
时间: 2023-07-10 21:39:38 浏览: 89
好的,以下是一个简单的自定义ContentProvider,实现了增、删、改、查操作:
```java
public class MyContentProvider extends ContentProvider {
private static final String AUTHORITY = "com.example.myprovider";
private static final String TABLE_NAME = "my_table";
private static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
private static final int CODE_ITEM = 1;
private static final int CODE_DIR = 2;
private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
URI_MATCHER.addURI(AUTHORITY, TABLE_NAME, CODE_DIR);
URI_MATCHER.addURI(AUTHORITY, TABLE_NAME + "/#", CODE_ITEM);
}
private SQLiteDatabase mDb;
@Override
public boolean onCreate() {
mDb = new MyDatabaseHelper(getContext()).getWritableDatabase();
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
@Nullable String[] selectionArgs, @Nullable String sortOrder) {
Cursor cursor;
switch (URI_MATCHER.match(uri)) {
case CODE_DIR:
cursor = mDb.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
break;
case CODE_ITEM:
String id = uri.getLastPathSegment();
cursor = mDb.query(TABLE_NAME, projection, "_id=?", new String[]{id}, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
switch (URI_MATCHER.match(uri)) {
case CODE_DIR:
return "vnd.android.cursor.dir/" + AUTHORITY + "." + TABLE_NAME;
case CODE_ITEM:
return "vnd.android.cursor.item/" + AUTHORITY + "." + TABLE_NAME;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
long id = mDb.insert(TABLE_NAME, null, values);
if (id > 0) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
int count;
switch (URI_MATCHER.match(uri)) {
case CODE_DIR:
count = mDb.delete(TABLE_NAME, selection, selectionArgs);
break;
case CODE_ITEM:
String id = uri.getLastPathSegment();
count = mDb.delete(TABLE_NAME, "_id=?", new String[]{id});
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
return count;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
@Nullable String[] selectionArgs) {
int count;
switch (URI_MATCHER.match(uri)) {
case CODE_DIR:
count = mDb.update(TABLE_NAME, values, selection, selectionArgs);
break;
case CODE_ITEM:
String id = uri.getLastPathSegment();
count = mDb.update(TABLE_NAME, values, "_id=?", new String[]{id});
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
return count;
}
}
```
需要注意的是,该ContentProvider的数据库操作都是在UI线程中进行的,不适合于大量数据的操作。如果需要进行大量数据的操作,需要使用异步线程来进行。
阅读全文