解决android - CursorLoader not updating after data change
2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
推荐:深入源码解析Android中Loader、AsyncTaskLoader、CursorLoader、LoaderManager
如果对Loader、AsyncTaskLoader、CursorLoader、LoaderManager等概念不明白或不知道如何使用Loader机制,可参见博文Android中Loader及LoaderManager的使用(附源
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.
android android-contentprovider android-cursorloader android-loadermanager|
this question edited May 23 '12 at 4:05 Alex Lockwood 64.5k 27 167 217 asked Oct 27 '11 at 11:11 akalipetis 515 1 5 13 6 do you call
getContext().getContentResolver().notifyChange(Uri,..);
in your update/insert ContentProvider methods implementation ? do you call
cursor.setNotificationUri(getContext().getContentResolver(), uri);
before return it from query method in your ContentProvider –
Selvin Oct 27 '11 at 11:19 Nope, I didn't, that was the problem! Thanks! :) –
akalipetis Oct 27 '11 at 12:19
|
3 Answers
3
解决方法
Did you call setNotificationUri(ContentResolver cr, Uri uri)
on the Cursor
before returning it in ContentProvider.query()
?
And did you call getContext().getContentResolver().notifyChange(uri, null)
in the 'insert' method of your ContentProvider
?
EDIT:
To get a ContentResolver
call getContext().getContentResolver()
in your ContentProvider
.
|
this answer edited Oct 27 '11 at 11:34 answered Oct 27 '11 at 11:18 thaussma 6,652 1 26 35 5 Annoying that this isn't mentioned when I followed the android dev docs to create a content provider – Blundell Mar 3 '13 at 12:32 17 Also, you should NOT call cursor.close() at ANY point in onLoadFinished else you will not receive any further updates to the underlying dataset. – Dororo May 8 '13 at 16:26 1 Does the URI passed to the setNotificationUri() match that of the notifyChange() URI exactly, or does simply have to have the same authority or something? – reubenjohn Dec 19 '14 at 12:25 1 The URIs have to be equal. – thaussma Dec 19 '14 at 12:40 1 @Herrmann Is there something else one should be aware of. Namely: I call both setNotificationUri(ContentResolver cr, Uri uri) and notifyChange(uri, null) with the same URI. Yet my list view is not updated. – f470071 Sep 23 '15 at 6:16 | show more comments
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.
|
this answer answered Sep 28 '16 at 11:49 ultraon 558 7 12
|
Accepted answer was little bit trick to understand so I am writing the answer to make it easy for other developes..
推荐:Android Loader(三) 结合CursorLoader分析Loader相关源码
Android Loader(二) CursorLoader Android Loader(四) 自定义Loader从网络中获取文本数据 首先从加载数据的过程开始分析。 初始化Loader的方法是:getLoaderMana
- Go to the class in which you have extended the
ContentProvider
Find the query() method which has 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
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor;
cursor = noticeDbHelper.getReadableDatabase().query(
NoticeContract.NoticeTable.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
sortOrder
);
//This line will let CursorLoader know about any data change on "uri" , So that data will be reloaded to CursorLoader
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}`
|
this answer answered Feb 22 at 12:01 ManishMenaria 435 4 13
|
原文: http://www.jb51.net/article/37767.htm http://write.blog.csdn.net/posteditref=toolbar android CursorLoader用法介绍 工作内容集中到Contact模块
原文: http://www.jb51.net/article/37767.htm http://write.blog.csdn.net/posteditref=toolbar android CursorLoader用法介绍 工作内容集中到Contact模块
相关阅读排行
- 1Android Https相关完全解析 当OkHttp遇到Https
- 2Android APK反编译详解(附图)
- 3[置顶] Android APK反编译就这么简单 详解(附图)
- 4Android Fragment 真正的完全解析(上)
- 5Android RecyclerView 使用完全解析 体验艺术般的控件