可以按照以下步骤进行操作:
以下是一个示例代码:
// 自定义ContentProvider类
public class MyContentProvider extends ContentProvider {
private SQLiteDatabase database;
@Override
public boolean onCreate() {
// 初始化数据库
DBHelper dbHelper = new DBHelper(getContext());
database = dbHelper.getWritableDatabase();
return true;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// 删除满足条件的行
int rowsDeleted = database.delete(TABLE_NAME, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return rowsDeleted;
}
// 其他方法的实现...
}
使用ContentResolver调用ContentProvider的delete()方法的示例代码如下:
// 调用ContentProvider的delete()方法
Uri uri = Uri.parse("content://com.example.mycontentprovider/mytable");
String selection = "column1 = ?";
String[] selectionArgs = {"value1"};
int rowsDeleted = getContentResolver().delete(uri, selection, selectionArgs);
在上述代码中,"com.example.mycontentprovider"是ContentProvider的授权标识,"mytable"是要操作的数据表名,"column1 = ?"是删除行的条件,"value1"是条件参数的值。
领取专属 10元无门槛券
手把手带您无忧上云