内容解析器notifyChange()不起作用 [英] content resolver notifyChange() not working
问题描述
我已经设置了一个片段,以使用CursorLoader从自定义内容提供程序中提取数据.
I have setup a fragment to pull data from a custom content provider using a CursorLoader.
问题在于,当我使用内容解析器更新SQLite表中的记录时,光标不会刷新,即getContext().getContentResolver().notifyChange(myUri, null)
没有任何作用.我必须退出片段,然后再次打开以查看更改.
The problem is that when i update a record in the SQLite table using the content resolver, the cursor does not refresh i.e. the getContext().getContentResolver().notifyChange(myUri, null)
has no effect. I have to exit the fragment and open it again to see the change.
我认为问题在于加载程序没有观察到我用来更新行的URI:
I think the problem is that the URI i have used to update a row is not being observed by the loader :
- 用于创建加载器的URI-
content://com.myapp.provider/MyTable/Set/22
- 用于更新行-
content://com.myapp.provider/MyTable/167
的URI
- URI to create loader -
content://com.myapp.provider/MyTable/Set/22
- URI to update row -
content://com.myapp.provider/MyTable/167
167标识表中的唯一行. 22标识表中的一组行. 是否可以通过某种方式告诉加载器行167位于集合22中,因此应该重置光标?
167 identifies a unique row in the table. 22 identifies a set of rows in the table. Is there some way to tell the loader that the row 167 comes within the set 22, so it should reset the cursor?
这里是代码,以防它变得更加清晰:
Here is the code in case it brings more clarity :
在片段中创建CursorLoader:
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle queryBundle) {
CursorLoader cursorLoader = new CursorLoader(getActivity(), Uri.parse("content://com.myapp.provider/MyTable/Set/22"), myProjection, null, null, null);
return cursorLoader;
}
在按钮上单击片段:
mContext.getContentResolver().update("content://com.myapp.provider/MyTable/167", values, null, null);
内容提供商类:
private static final String AUTHORITY = "com.myapp.provider";
private static final String TABLE_PATH = "MyTable";
public static final String CONTENT_URI_BASEPATH = "content://" + AUTHORITY + "/" + TABLE_PATH;
private static final int URITYPE_TABLE = 1;
private static final int URITYPE_SINGLE_SET = 2;
private static final int URITYPE_SINGLE_ROW = 3;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMatcher.addURI(AUTHORITY, TABLE_PATH,URITYPE_TABLE);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/Set/#", URITYPE_SINGLE_SET);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/#", URITYPE_SINGLE_ROW);
}
@Override
public int update(Uri myUri, ContentValues values, String selection, String[] selectionArgs){
int rowCount = 0;
String id;
SQLiteDatabase db = localDB.getWritableDatabase();
int uriType = sUriMatcher.match(myUri);
switch(uriType){
case URITYPE_SINGLE_ROW :
id = uri.getLastPathSegment();
//selection and selectionArgs are ignored since the URI itself identifies a unique row.
rowCount = db.update(MyTable.TABLE_NAME, values, MyTable.COLUMN_ID + " = ?", new String[] {id});
}
getContext().getContentResolver().notifyChange(myUri, null);
return rowCount;
}
推荐答案
解决方案是在要观察的Uri(即集合)而不是行上调用notifyChange()
.
The solution is to call notifyChange()
on the Uri that is being observed i.e. the set and not on the row.
要实现这一点,我们需要进行一些更改:
To achieve this, we need to make some changes :
-
在调用更新时将设置ID包含在URI中:
Include the set ID in the URI when calling the update :
mContext.getContentResolver().update("content://com.myapp.provider/MyTable/Set/22/167", values, null, null);
将单行的URI模式从"/#"更改为"/Set/#/#"
Change the URI pattern of a single row from "/#" to "/Set/#/#"
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMatcher.addURI(AUTHORITY, TABLE_PATH,URITYPE_TABLE);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/Set/#", URITYPE_SINGLE_SET);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/Set/#/#", URITYPE_SINGLE_ROW);
}
然后在更新功能中,构造一个必须通知的新Uri:
Then in the update function, construct a new Uri that has to be notified :
List<String> pathSegments = uri.getPathSegments();
String mySetID = pathSegments.get(2);
Uri mySetUri = Uri.parse("content://" + AUTHORITY + "/" + TABLE_PATH + "/Set/" + mySetID);
getContext().getContentResolver().notifyChange(mySetUri, null);
这篇关于内容解析器notifyChange()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!