CursorLoader does not update after changing data

I created a small application trying to understand the functionality of the LoaderManager and CursorLoader .

I implemented LoaderCallbacks<Cursor> in my FragmentActivity class and everything works fine, except for the fact that when I update my data using the ContentResolver.update() or ContentResolver.insert() onLoadFinished() , onLoadFinished() not called and as a result my data is not updated.

I have a custom ContentProvider, and I am wondering if there is no problem in my ContentProvider not notifying that the data has changed or something else.

+49
android android-contentprovider android-loadermanager android-cursorloader
Oct 27 '11 at 11:11
source share
4 answers

Did you call setNotificationUri(ContentResolver cr, Uri uri) in Cursor before returning it to ContentProvider.query() ?

And you called getContext().getContentResolver().notifyChange(uri, null) in the "insert" method of your ContentProvider ?

EDIT:

To get the ContentResolver call getContext().getContentResolver() in the ContentProvider .

+104
Oct 27 '11 at 11:18
source share

Also check that you are calling cursor.close () somewhere, because in this case you will unregister the contents registered by CursorLoader. And closing the cursor is controlled by CursorLoader.

+4
Sep 28 '16 at 11:49
source share

The accepted answer was a little difficult to understand, so I am writing an answer to make it easy for other developers.

  • Go to the class where you extended ContentProvider
  • Find the query () method that has the following syntax

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

  • Write this line where you return the cursor

    cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor;

At the end, my query method is as follows:

 @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; }` 
+3
Feb 22 '17 at 12:01
source share

Can someone explain to me a simple way to understand how this next method works

 cursor.setNotificationUri(getContext().getContentResolver(), uri); getContext().getContentResolver().notifyChange(uri, null) 

and what is the relationship between them? thank

0
Dec 05 '18 at 17:47
source share



All Articles