Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a CursorAdapter tied to listview when number of items changes?

I have a ListView backed by a custom adapter based on a CursorAdapter.

The cursor I give it is based on a list of Notes in the database (each note is a DB row).

Everything works just fine until I delete a note from the database. I'm not sure how to properly update the cursor/adapter/listview to display the new data.

I've read conflicting posts on using adapter.notifyDataSetChanged() vs cursor.requery() to refresh the UI, but neither of them seems to work in this case. The only way I've been able to refresh it is by creating a new cursor from a new query and calling adapter.changeCursor(newCursor).

Could someone demonstrate the proper way to update the backing data and then the UI (with psuedocode if possible).

like image 328
CodeFusionMobile Avatar asked Aug 16 '10 18:08

CodeFusionMobile


2 Answers

You have to requery and then notifyDataSetChanged.

like image 131
fedj Avatar answered Sep 28 '22 07:09

fedj


Calling requery will re-execute the exact query that was used to create the cursor - that is, it won't re-execute the actual method in your code. So if your method contains dynamic stuff such as sorting based on a preference, it won't be updated. In this case, you don't want to use the same query, you want a different one.

like image 42
JRL Avatar answered Sep 28 '22 07:09

JRL