数据更改后 CursorLoader 不更新
我创建了一个小型应用程序,试图了解 LoaderManager 和 CursorLoader 类的功能。
我已经在我的 FragmentActivity
类上实现了 LoaderCallbacks
,一切正常,除了当我通过 ContentResolver.update()< 更新数据时/code> 或
ContentResolver.insert()
-方法,onLoadFinished()
未调用,因此我的数据不会更新。
我有一个自定义的 ContentProvider,我想知道问题是否出在我的 ContentProvider 中,没有通知数据更改或其他原因。
I have created a small application, trying to understand the functionality of the LoaderManager
and CursorLoader
-classes.
I have implemented LoaderCallbacks<Cursor>
on my FragmentActivity
-class and everything works fine, except the fact that when I update my data via ContentResolver.update()
or ContentResolver.insert()
-methods, onLoadFinished()
is not called and as a result my data doesn't update.
I have a custom ContentProvider and I am wondering if the problem is in my ContentProvider not notifying that the data changed or something else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否在
Cursor
上调用了setNotificationUri(ContentResolver cr, Uri uri)
,然后才在ContentProvider.query()
中返回它?您是否在
ContentProvider
的“insert”方法中调用了getContext().getContentResolver().notifyChange(uri, null)
?编辑:
要获取
ContentResolver
,请在ContentProvider
中调用getContext().getContentResolver()
。Did you call
setNotificationUri(ContentResolver cr, Uri uri)
on theCursor
before returning it inContentProvider.query()
?And did you call
getContext().getContentResolver().notifyChange(uri, null)
in the 'insert' method of yourContentProvider
?EDIT:
To get a
ContentResolver
callgetContext().getContentResolver()
in yourContentProvider
.还要检查您是否在某处调用了cursor.close(),因为在这种情况下您取消注册了由CursorLoader注册的内容观察器。游标的关闭由CursorLoader 管理。
Also check if you call somewhere cursor.close(), because in this case you unregister the content observer which was registered by CursorLoader. And the cursor closing is managed by CursorLoader.
接受的答案有点难以理解,因此我编写答案是为了方便其他开发人员。
ContentProvider
Find the query() 具有以下语法的方法
public Cursor query(Uri uri, String[]projection,Stringselection,String[]selectionArgs,StringsortOrder)
在要返回光标的位置写入此行
cursor.setNotificationUri(getContext().getContentResolver(), uri);
returncursor;
最后我的查询方法是这样的
Accepted answer was the little bit tricky to understand so I am writing the answer to make it easy for other developers..
ContentProvider
Find the query() method which has the following syntax
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Write this line where you are returning the cursor
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
In the end, my query method looks like this