CursorLoader not updating after data change

前端 未结 3 849
时光说笑
时光说笑 2020-11-29 00:25

I have created a small application, trying to understand the functionality of the LoaderManager and CursorLoader-classes.

I have implemente

相关标签:
3条回答
  • 2020-11-29 00:42

    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.

    0 讨论(0)
  • 2020-11-29 01:02

    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;
    }`
    
    0 讨论(0)
  • 2020-11-29 01:08

    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.

    0 讨论(0)
提交回复
热议问题