How to update a CursorAdapter linked to a list when the number of elements changes?

I have a ListView supported by a CursorAdapter custom adapter.

The cursor that I give him is based on the Notes list in the database (each note is a DB string).

Everything works fine until I delete the note from the database. I am not sure how to update the cursor / adapter / list correctly to display new data.

I read conflicting messages when using adapter.notifyDataSetChanged() vs cursor.requery() to update the user interface, but none of them seem to work in this case. The only way I was able to update it was to create a new cursor from a new request and call adapter.changeCursor(newCursor) .

Can anyone demonstrate a proper way to update the backup data, and then the user interface (using psuedocode if possible).

+6
android android-listview listview
source share
3 answers

You must requery and then notifyDataSetChanged .

+9
source share

Calling requery will re-execute the exact query that was used to create the cursor, i.e. it will not re-execute the actual method in your code. Therefore, if your method contains dynamic materials, such as sorting based on preferences, it will not be updated. In this case, you do not want to use the same query, you want a different one.

+1
source share

This is what works for me, I'm not sure if this is the best way.

 c = db.rawQuery( "SELECT * FROM mytable", null); //same line of the first initialization adapter.swapCursor(c); 

I am updating a single cursor, I do not know what to do with the new one. Also I don't understand pepole responding with the function name.

0
source share

All Articles