Home » Android » android – How to refresh a ListView to requery its cursor to repopulate its data and its views

android – How to refresh a ListView to requery its cursor to repopulate its data and its views

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a ListView which uses a CursorAdatper as its adapter.
I would like to have the list view to

  • requery its data
  • refresh its view once the requery is done.

I tried:

CursorAdapter adapter = (CursorAdapter)listView.getAdapter();
adapter.notifyDataSetChanged();

And i tried:

CursorAdapter adapter = (CursorAdapter)listView.getAdapter();
adapter.getCursor().requery();

but none worked. I have set a break point in my ContentProvider‘s query method, but I don’t see the requery being called or my ListView getting refreshed with new data.

Can you please tell me what is the solution to my problem?

Thank you.

How to&Answers:

Calling requery() on a database or content provider Cursor attached to ListView via a CursorAdapter automatically refreshes the list (your second scenario above). You can see some examples of this. If that is not working for you, there may be some bug in your ContentProvider or adapter.

Answer:

In the new API (using loader manager) you can use:

getLoaderManager().getLoader(_YOUR_LOADER_ID_).forceLoad();

Answer:

you can try: adapter.changeCursor(new cursor). It work for me

Answer:

I know this answer is probably too late but fyi to others, if you are using a cursor loader (as you probably should do instead), just do

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

where uri is the same uri used for the cursor loader.