android - 在 Android 中使用 Content Provider 批量删除项目

标签 android android-contentprovider android-contentresolver

我正在尝试批量删除表格中的一些项目。

    String ids = { "1", "2", "3" };

    mContentResolver.delete(uri, MyTables._ID + "=?", ids);

但是我不断收到以下错误

java.lang.IllegalArgumentException: Too many bind arguments. 3 arguments were provided but the statement needs 1 arguments.

最佳答案

您可以使用 ContentProviderOperation用于在一个事务中批量删除/插入/更新。您不必连接字符串会更好。它也应该非常有效。删除:

    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation operation;

    for (Item item : items) {

        operation = ContentProviderOperation
                .newDelete(ItemsColumns.CONTENT_URI)
                .withSelection(ItemsColumns.UID + " = ?", new String[]{item.getUid()})
                .build();

        operations.add(operation);
    }

    try {
        contentResolver.applyBatch(Contract.AUTHORITY, operations);
    } catch (RemoteException e) {

    } catch (OperationApplicationException e) {

    }

关于android - 在 Android 中使用 Content Provider 批量删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11458707/

相关文章:

android - 增加android中 ListView 中项目之间的间距

java - 可以解析内容uri

android - ContentProvider线程

Android:像 WhatsApp 一样监听联系人变化

android - 强制用户拥有最新版本的应用程序

android - 在android上的报警 channel 上播放声音

java - 除以零,同时在 Android 的表格布局中动态添加列

java - Cursor.moveToNext 错误

android - 在 Android Q 上使用 Glide 显示来自具有 "content://"方案的 URI 的图像

android - 无法使用ContentResolver查询方法,NoSuchMethodError