开发者

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).


You have to requery and then notifyDataSetChanged.


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.


This is what works for me, im not sure its the best way.

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

I refresh the only cursor, I dont know what to do with a new one. Also i dont understand pepole that answer with a name of a function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜