views:

1327

answers:

1

I have an android list activity that is backed by a database cursor through a SimpleCursorAdapter.

When the items are clicked, a flag field in the coresponding row in the database is toggled and the view in the list needs to be updated.

The problem is, when the view that's updated goes off screen and is recycled, the old value is displayed on the view when it returns into view. The same thing happens whenever thr list is redrawb (orientation changes, etc).

I use notifydatasetchanged() to refresh the cursor adapter but it seems ineffective.

How should I be updating the database so the cursor is updated as well?

+8  A: 

Call requery() on the Cursor when you change data in the database that you want reflected in that Cursor (or things the Cursor populates, like a ListView via a CursorAdapter).

A Cursor is akin to an ODBC client-side cursor -- it holds all of the data represented by the query result. Hence, just because you change the data in the database, the Cursor will not know about those changes unless you refresh it via requery().

CommonsWare
So what does notifydatachanged do then?
CodeFusionMobile
There is no "notifydatachanged". If you mean notifyDataSetChanged() on Adapter, that is how the SimpleCursorAdapter tells the ListView that data was changed. To quote from the documentation, "Notifies the attached View that the underlying data has been changed and it should refresh itself." However, your problem is not with the Adapter telling the ListView about the change -- your problem is that the Adapter doesn't know the data has changed. Calling requery() is the way to address that with a CursorAdapter.
CommonsWare
That makes sense now. I misunderstood the data flow.
CodeFusionMobile