注册 | 登录

解决android - CursorLoader not updating after data change

itPublisher 分享于





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


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?


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

  1. Go to the class in which you have extended the ContentProvider
  2. Find the query() method which has following syntax

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

  3. 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

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Cursor cursor;
    cursor = noticeDbHelper.getReadableDatabase().query(
    //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


推荐:android CursorLoader用法介绍

原文: android CursorLoader用法介绍 工作内容集中到Contact模块








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱