[英]ContentProvider update() doesn't work
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()
中update()
的行数。 这里似乎有什么问题?
Switch中没有更新代码。 我想你错过了
为了使用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(...);
请让我知道这是否适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.