Christopher Francisco
Christopher Francisco

Reputation: 16278

Batch delete selected items on ListView/GridView

Following the guide found here http://developer.android.com/guide/topics/ui/menus.html#CAB I went up to a dead end on how to remove all the selected items from the listView's adapter.

In the guide it is shown as a method called deleteSelectedItems(); but since it is never implemented, I got stuck. How can I do this?

Upvotes: 1

Views: 385

Answers (1)

user1178729
user1178729

Reputation:

I asume you are using a List. Do the following:

private void deleteSelectedItems() {
   SparseBooleanArray checked = mListView.getCheckedItemPositions();+
   List<YourObject> list = mListOfObjects;
   for (int i = 0; i < mListView.getCount(); i++)
        if (checked.get(i)) 
            YourObject item = list.get(i);
            mListOfObjects.remove(item); //or whatever you want to do with it.
}

Upvotes: 1

Related Questions