49

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
akalipetis
  • 938
  • 1
  • 7
  • 14
  • 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 Answers3

105

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.

thaussma
  • 9,756
  • 5
  • 44
  • 46
  • Thanks for the fast reply!Seems like this is the problem, how can I get a ContentResolver from the ContentProvider to put it in setNotificationUri()? EDIT: I found it, just use getContext().getCntentProvider() – akalipetis Oct 27 '11 at 11:31
  • 5
    Annoying that this isn't mentioned when I followed the [android dev docs](http://developer.android.com/guide/topics/providers/content-provider-creating.html) to create a content provider – Blundell Mar 03 '13 at 12:32
  • 23
    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 08 '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
  • YES, YES, YES!! Wish I'd seen this Q&A 24 hours ago - but still very happy to have finally got this nailed! – ban-geoengineering Dec 20 '14 at 19:45
  • 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 06:16
  • Hmm, do you use a custom cursor? Have you implemented a ContentProvider? – thaussma Sep 23 '15 at 15:00
  • @Herrmann Please take a look at http://stackoverflow.com/questions/32741634/how-to-make-notifychange-work-between-two-activities. – f470071 Sep 23 '15 at 18:41
  • And you should use getContext().getContentResolver().notifyChange(uri, null) whenever you update your data. I mean insert update delete. – Sergey Dirin Feb 09 '18 at 04:45
5

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.

ultraon
  • 2,220
  • 2
  • 28
  • 27
4

Accepted answer was the little bit tricky to understand so I am writing the answer to make it easy for other developers..

  1. Go to the class in which you have extended the ContentProvider
  2. Find the query() method which has the 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

@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;
}`
Manish Menaria
  • 23,899
  • 3
  • 21
  • 23