我已經實現的ContentProvider
update()
和使用getContext().getContentResolver().notifyChange(uri, null);
有NotifyChange與改變的URI從contentProvider.update()
我顯然需要通知到觀察者的是,每當只有一個行影響我想與行特定的URI通知,但無法找到辦法。 像"select id where selectionArgs"
可以做到這一點,但這將是一個愚蠢的方式。
onchange(boolean, uri)
得到完整的uri而不是特定的行,很容易理解,這是因爲ContentProvider.update()
發送的是相同的。的MyContentProvider
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Log.d("TAG", "update " + uri.getPath());
int count = 0;
switch (uriMatcher.match(uri)) {
case BOOKS:
count = booksDB.update(DATABASE_TABLE, values, selection, selectionArgs);
break;
case BOOK_ID:
count = booksDB.update(DATABASE_TABLE, values,
_ID + " = " + uri.getPathSegments().get(1)
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""),
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (count == 1) {
Cursor c = query(uri, new String[] { _ID }, selection, selectionArgs, null);
long rowId = Long.valueOf(c.getString(c.getColumnIndex(_ID)));
uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
我會更新表
some code for more clarity
update()方法的一些怎麼樣的
getContentResolver().update(MyContentProvider.CONTENT_URI, values1, MyContentProvider._ID+"<?", new String[]{"3"}));
坦率地說,代碼幾乎沒有涉及到問題,只是舉你有些情況下
有些代碼可能是有用的ID。你如何更新許多行? – sandrstar 2013-03-26 08:42:08
確保你對內容提供者的query()方法返回的遊標調用'setNotificationUri(ContentResolver cr,Uri uri)'。更多信息在http://stackoverflow.com/questions/7915050/cursorloader-not-updating-after-data-change。 – 2014-12-20 20:34:57