Android : How can I efficiently transfer checked items from a listview to another activity when data is supplied by a Content Provider

on Thursday, October 9, 2014


The title doesn't really cover it all. It's basically the following use case:



  • Select a bunch of items in a listview

  • Move to the next activity

  • Show the selected items in this new activity in a listview

  • If the user decides to move to the previous section the same items should still be checked


If I were using POJOs I could simply add an OnItemClickListener to the ListView and add the items to an array each time user clicks an item and ship this array to the new Activity. However the ListView is populated using a Loader which gets the data from a ContentProvider and puts it in a SimpleCursorAdapter.


In the current state I can think of a few possible solutions:


1) Add an extra column to the relevant table of the objects in the ListView. Each time the user selects an item this column is updated to indicate it is selected.


This has a few advantages:



  • Once I have moved to the next Activity I can simply query for all selected items.

  • When moving to the previous activity I can use this column to show a selected view.


But also a few disadvantages:



  • Requires an update each time an item is clicked, triggering the loader.

  • Requires a custom adapter which uses the state of the new column to decide whether it should or should not be shown as checked. Possibly creating a delay between clicking an item and actually showing it as checked.

  • The default check options in the ListView will be unusable


2) Track the IDs of checked items (using OnItemClickedListener or getCheckedItemIds) and pass them to the next Activity. In the new activity I then create a selection argument of "ID = ? OR " repeating N times (and excluding the last OR) and use the given array as the selection arguments.


Advantages:



  • No need to keep updating the database.

  • No extra columns in the database.

  • Item checking has no extra delay.

  • Default check options from the listview still usable


Disadvantages:



  • Moving to the previous activity is now harder. I could return the list of selected item IDs, but the listview only has the option setItemChecked which takes a position. So I'd have to iterate over the entire adapter to find the positions of the items and set them as checked.


I'm probably capable of implementing them without any hassle. At the moment I'm gravitating towards the second option.


So ultimately I have the following questions:



  1. Is there an easier way to do this in Android.

  2. What would be a good way to recheck the items (see the disadvantage in the second suggestion and if there's no better way to do it).


This ListView will also get a search function which will probably again make it a bit harder because if I'm not mistaken filtering resets it every time. So I'd also have to recheck items every time (ideally during filtering).


0 comments:

Post a Comment