android studio 写一个简单的内容提供者代码模板
时间: 2023-10-05 22:07:36 浏览: 46
好的,以下是一个简单的内容提供者代码模板:
```
public class MyContentProvider extends ContentProvider {
// 定义常量,用于 UriMatcher 匹配 Uri
private static final int TABLE1 = 1;
private static final int TABLE1_ID = 2;
// 定义 UriMatcher
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sUriMatcher.addURI("com.example.myapp.provider", "table1", TABLE1); // 匹配整张表
sUriMatcher.addURI("com.example.myapp.provider", "table1/#", TABLE1_ID); // 匹配表中某条记录,# 表示数字
}
// 定义数据库帮助类
private MyDatabaseHelper mDatabaseHelper;
@Override
public boolean onCreate() {
mDatabaseHelper = new MyDatabaseHelper(getContext(), "myapp.db", null, 1);
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Cursor cursor = null;
switch (sUriMatcher.match(uri)) {
case TABLE1:
cursor = db.query("table1", projection, selection, selectionArgs, null, null, sortOrder);
break;
case TABLE1_ID:
String id = uri.getPathSegments().get(1);
cursor = db.query("table1", projection, "id=?", new String[]{id}, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
return cursor;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
Uri returnUri = null;
switch (sUriMatcher.match(uri)) {
case TABLE1:
long id = db.insert("table1", null, values);
returnUri = ContentUris.withAppendedId(uri, id);
break;
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
return returnUri;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int count = 0;
switch (sUriMatcher.match(uri)) {
case TABLE1:
count = db.update("table1", values, selection, selectionArgs);
break;
case TABLE1_ID:
String id = uri.getPathSegments().get(1);
count = db.update("table1", values, "id=?", new String[]{id});
break;
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
return count;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
int count = 0;
switch (sUriMatcher.match(uri)) {
case TABLE1:
count = db.delete("table1", selection, selectionArgs);
break;
case TABLE1_ID:
String id = uri.getPathSegments().get(1);
count = db.delete("table1", "id=?", new String[]{id});
break;
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
return count;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
switch (sUriMatcher.match(uri)) {
case TABLE1:
return "vnd.android.cursor.dir/table1";
case TABLE1_ID:
return "vnd.android.cursor.item/table1";
default:
throw new IllegalArgumentException("Unknown Uri: " + uri);
}
}
}
```
其中,`com.example.myapp.provider` 是你自己定义的内容提供者的 authority,你需要将其替换为你自己的 authority。`table1` 是你自己定义的表名,你需要将其替换为你自己的表名。在实际使用中,你还需要根据自己的需求对代码进行调整。