4.21
所花时间(包括上课):3.5
打码量(行):500
博客量(篇):1
了解到知识点:学习contentProvider
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MyContentProvider extends ContentProvider {
// ContentProvider 的 URI 标识
private static final String AUTHORITY = "com.example.mycontentprovider";
private static final String PATH = "items";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PATH);
private static final int ITEMS = 1;
private static final int ITEM_ID = 2;
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, PATH, ITEMS);
uriMatcher.addURI(AUTHORITY, PATH + "/#", ITEM_ID);
}
private SQLiteDatabase database;
@Override
public boolean onCreate() {
// 初始化数据库
DatabaseHelper dbHelper = new DatabaseHelper(getContext());
database = dbHelper.getWritableDatabase();
return database != null;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
// 处理查询请求
Cursor cursor;
switch (uriMatcher.match(uri)) {
case ITEMS:
cursor = database.query(DatabaseHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
break;
case ITEM_ID:
selection = DatabaseHelper.COLUMN_ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
cursor = database.query(DatabaseHelper.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("未知的 URI: " + uri);
}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
// 处理插入请求
long rowId = database.insert(DatabaseHelper.TABLE_NAME, null, values);
if (rowId > 0) {
Uri newUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(newUri, null);
return newUri;
}
throw new SQLException("插入行失败: " + uri);
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
// 处理更新请求
int count;
switch (uriMatcher.match(uri)) {
case ITEMS:
count = database.update(DatabaseHelper.TABLE_NAME, values, selection, selectionArgs);
break;
case ITEM_ID:
selection = DatabaseHelper.COLUMN_ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
count = database.update(DatabaseHelper.TABLE_NAME, values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("未知的 URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
// 处理删除请求
int count;
switch (uriMatcher.match(uri)) {
case ITEMS:
count = database.delete(DatabaseHelper.TABLE_NAME, selection, selectionArgs);
break;
case ITEM_ID:
selection = DatabaseHelper.COLUMN_ID + "=?";
selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
count = database.delete(DatabaseHelper.TABLE_NAME, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("未知的 URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
// 返回 MIME 类型
switch (uriMatcher.match(uri)) {
case ITEMS:
return "vnd.android.cursor.dir/vnd.com.example.mycontentprovider.items";
case ITEM_ID:
return "vnd.android.cursor.item/vnd.com.example.mycontentprovider.items";
default:
throw new IllegalArgumentException("未知的 URI: " + uri);
}
}
// SQLiteOpenHelper 子类,用于管理数据库创建和版本管理
private static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "items";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_VALUE = "value";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_NAME + " TEXT NOT NULL, "
+ COLUMN_VALUE + " TEXT NOT NULL);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建数据库表
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 处理数据库升级
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultTextView = findViewById(R.id.result_text_view);
// 获取按钮视图
Button insertButton = findViewById(R.id.insert_button);
Button queryButton = findViewById(R.id.query_button);
// 设置插入数据按钮点击监听器
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertData();
}
});
// 设置查询数据按钮点击监听器
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
queryData();
}
});
}
private void insertData() {
// 插入数据到 ContentProvider
ContentValues values = new ContentValues();
values.put("name", "Example Name");
values.put("value", "Example Value");
Uri newUri = getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
resultTextView.setText("Inserted: " + newUri);
}
private void queryData() {
// 从 ContentProvider 查询数据
Cursor cursor = getContentResolver().query(MyContentProvider.CONTENT_URI, null, null, null, null);
if (cursor != null) {
StringBuilder result = new StringBuilder();
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String value = cursor.getString(cursor.getColumnIndex("value"));
result.append("Name: ").append(name).append(", Value: ").append(value).append("\n");
}
cursor.close();
resultTextView.setText(result.toString());
}
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/18138322
千万千万赵千万