Search code examples
androiddatabaselistviewrowandroid-cursoradapter

Android: Modify CursorAdapter without Changing Database?


I have a nicely working custom CursorAdapter with customized ListView. This view allows the user to play around with their UI by re-arranging the rows of the ListView. No problem (well, now after a lot of research and trial and error!).

But I don't like having to hit the database every time the user moves one of these rows. They could be doing quite a bit of modifications. Furthermore, I would like the ability for the user to change their mind and cancel all their changes.

So I am wondering: is there a way to modify the contents of this ListView (ie, the orders of the rows) without having to change the database? That way I could update the database when the user clicks OKAY in one swoop and ignore those changes if they cancel.

Also with this theoretical technique, the user only has to wait ONCE for the AsyncTask to pop and do its database stuff instead of every single time they modify their list.


Solution

  • Use a custom class having all the members you need from the database. Create a ArrayList of the customClass object and fill it with the data from cursor retrieved from the database. Pass this arraylist to a CustomBaseAdapter and populate the listview. Modify the arraylist data according to the user reactions. Based on OK or CANCEL click update the corresponding data in the database.