【问题标题】:ContentProvider update() doesn't work内容提供者更新()不起作用
【发布时间】:2018-08-25 13:28:21
【问题描述】:

update() 方法的实现可能有问题,但我不确定是什么:

 @Override
 public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        int count = 0;
        switch (uriMatcher.match(uri)) {
            case uriCode:
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
 }

我是这样调用方法的:

Random r1 = new Random();
long insertValue1 = r1.nextInt(5);
updatedValues.put(Provider.adPoints, insertValue1);
int value = getContentResolver().update(Provider.CONTENT_URI, updatedValues, null, null); //insert(Provider.CONTENT_URI, values);

我还想利用count 并返回update() 中更新的行数。这里似乎有什么问题?

【问题讨论】:

    标签: java android android-contentprovider insert-update


    【解决方案1】:

    为了使用 Content Provider 获取行更新,您必须这样做:

    首先确保您在 ContentProvider 中有对 DatabaseHelper 类的引用:

    private YourDatabaseSQLLiteHelper mDbHelper;
    
    @Override
    public boolean onCreate() {
        mDbHelper = new YourDatabaseSQLLiteHelper(getContext());
        return true;
    }
    

    然后覆盖内容提供者的 update() 方法:

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues cv, @Nullable String selection, @Nullable String[] selectionArgs) {
        int rowsUpdated;
    
        switch (sUriMatcher.match(uri)) {
    
            case YOUR_TABLE_CODE:
    
                // This is what you need.
                rowsUpdated = mDbHelper.getWritableDatabase().update(
                        YourTableContract.YourTableEntry.TABLE_NAME,
                        cv,
                        selection,
                        selectionArgs);
                break;
    
            default:
                throw new UnsupportedOperationException("Unknown uri: " + uri);
        }
    
        if (rowsUpdated != 0)
            getContext().getContentResolver().notifyChange(uri, null);
    
        return rowsUpdated;
    }
    

    然后当你调用该方法时,它必须返回更新了多少行。

    int rowsUpdatedCount = getContentResolver().update(...);
    

    如果这对你有用,请告诉我。

    【讨论】:

    • 抱歉之前没有提到,但我的是一个非 db ContentProvider。我使用了一个自定义 Item 类,在其中设置值并通过 ArrayList 将它们添加到 ContentProvider,例如ArrayList<Item>。但我非常理解你所说的代码的意思。这应该可行!
    【解决方案2】:

    Switch 中没有更新代码。我想你错过了

    【讨论】:

    • 更新代码是指getContext().getContentResolver().notifyChange()吗?
    • 类似于 database.update(DatabaseHelper.TABLE_NAME, values, condition , selectionArgs).. 只有中断存在
    • 请参阅此链接以了解有关内容提供商的更多信息grokkingandroid.com/…
    • 事实证明,update() 方法永远不会在我的 ContentProvider 实现中运行。当我调用getContentResolver().update时,它直接调用ContentResolver中的update()方法。但是,我也直接调用insert() 方法,但insert() 方法确实通过了我的ContentProvider 实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    相关资源
    最近更新 更多