I have a database, a ListView, and a CustomCursorAdapter that extends CursorAdapter. A menu button adds an item to the database. I want the ListView to update and show this change. Normally it doesn’t show this new item until i go to the homescreen and reopen the application.
I did eventually get it to work by calling cursor.requery() or mCustomCursorAdapter.changeCursor(newCursor) whenever I added a new item, but when I set autoRequery to false in the CursorAdapter constructor, it worked just the same. Why does it update correctly when autoRequery is set to false?
Am I using CursorAdapter correctly? What is the standard way of keeping the list updated with the database? And what does autoRequery do?
The idiomatic and imho correct way to automatically update
Cursors is to callCursor#setNotificationUriwhen they are created and before they are handed off to whatever requested them. Then callContentResolver#notifyChangewhen anything in thatCursor‘s Uri’s namespace changes.For example, suppose you were creating a simple mail application and you wanted to update when new mail arrived but also provide various views on the mail. I’d have some basic Uri’s defined.
Now, say I wanted to get a cursor that gave me all mail and be updated when new mail arrives:
Now new mail arrives so I notify:
I should also notify all the
Cursors that selected for labels this new message metAnd also, maybe a cursor is viewing that one specific message so notify them as well:
The
getContentResolver()calls happen where the data is accessed. So if it’s in aServiceorContentProviderthat is where yousetNotificationUriandnotifyChange. You should not be doing that from where the data is accessed, e.g., anActivity.AlarmProvideris a simpleContentProviderthat uses this method to updateCursors.