ListView won't update even if I call notifyChange(uri, null); though content provider

136 views Asked by At

I'm having trouble trying to get my ListView to refresh when new data gets added to the content provider.

Reading similar posts on this issue, I made sure to use getContext().getContentResolver().notifyChange(uri, null); on my insert method and c.setNotificationUri(getContext().getContentResolver(), uri); on my query method (talking about the content provider class).

I implement LoaderManager.LoaderCallbacks<Cursor> in my activity:

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(ChatWindowActivity.this,
                                DatabaseContentProvider.getConversationUri(conversationId),
                                PROJECTION, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        switch (loader.getId()) {
            case LOADER_ID:
                mAdapter.swapCursor(cursor);
                break;
            }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }
}

And I'm using a custom cursor adapter with getItemViewType,getViewTypeCount,newView and bindView overridden (class extends CursorAdapter)

And in my service I call service.getContentResolver().insert(...,values), yet the activity ListView will not update unless I close and reopen the app.

What could be causing this?

1

There are 1 answers

0
ᴘᴀɴᴀʏɪᴏᴛɪs On BEST ANSWER

I've managed to solve this but posting here in case it helps someone in the future.

The problem was with getContext().getContentResolver().notifyChange(uri, null);. The content provider will notify that a change occured on the specific URI only.

So I had to make sure the CursorLoader and the insert call worked on exactly the same URI for this to work. Even a small difference like '/contacts/1' to '/contacts' would not notify the cursor to that it changed.