1

How to delete the checked Items in a multiple choice listview with a contextual action for deletion -

ArrayList<String> liveNames = new ArrayList<String>() {
        {
            add("dani");
            add("john");
            add("dave");
            add("alen");
            add("deno");
            add("feliks");
            add("jupi");

        }
    };

adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, liveNames);

    setListAdapter(adapter);

.......

 @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            lv = getListView();
            // Respond to clicks on the actions in the CAB
            switch (item.getItemId()) {
                case R.id.item1:
                    if(lv.getCheckedItemCount() > 0){
                        removeItems = lv.getCheckedItemIds();
                        deleteSelectedItems(removeItems);
                    }
                    mode.finish(); // Action picked, so close the CAB
                    return true;

                default:
                    return false;
            }
        }

now how should I implement it in the deleteSelectedItems(long[] delItms) method so that the selected item IDs from the ListView be deleted inside the "names" ArrayList. Please for some hints

I know that I can update the adapters list with

adapter.notifyDataSetChanged();

but how to get the positions of the items inside the listview with their IDs so I could just

name.remove(position) - but I have only the IDs.

Thanks

ArloGrant
  • 249
  • 5
  • 15

2 Answers2

3

This method should do the trick for you, I guess:

/* returns item's position in ArrayList by ID */
public int getItemPositionById(int id)
{
   for(int i = 0; i < names.size(); i++) {
      if(names.get(i).getId() == id) {
         return i;   // if found item with specified id, return it's position
      }
   }
   return -1;        // didn't find item with specified id
}

Just call it for all the ids you have and store those positions somewhere. Then you can remove all items at those positions.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
  • 1
    only a litle change - if(lv.getItemIdAtPosition(i) == id) { and instead names.size () put lv.getCount() ....lv is the listview instance – ArloGrant Feb 10 '13 at 11:19
1

Anyway I could not retrieve the checked Ids with this method

removeItems = lv.getCheckedItemIds(); 

because the the adapter needs to have stable Ids ...or something like that

so I tried to retrieve the positions of checked items with

SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();

and then to delete them and update the arraylist and the adapter

    public void removeSelectedItems(){
    int count = lv.getCount();
    SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
    for (int i=0;i<count;i++){
        if(checkedItemPositions.get(i))


        Log.e("TEST", liveNames.get(i));
        liveNames.remove(i);
    }

    adapter.notifyDataSetChanged();
}

The problem is I suppose with the liveNames ArrayList which dynamically changes its element indexes every time I remove one element so the end results come wrong.

Here is a link of a discussion for this type of problem but without a solution - How to get Selected items from Multi Select List View

HOW I SOLVED THE ISSUE:

  1. Created a second ArrayList instance
  2. Updated that ArrayList instance with the UNCHECKED items
  3. added it to the my listadapter - here is the method

    public void removeSelectedItems(){
    
    updatedList = new ArrayList<String>(); //initialize the second ArrayList
    
    int count = lv.getCount();  //number of my ListView items
    SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
    for (int i=0;i < count;i++){
        if(!checkedItemPositions.get(i))
    
        updatedList.add(liveNames.get(i));  
        Log.e("TEST", liveNames.get(i));
    }
    
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, updatedList);
    
    setListAdapter(adapter);}
    

Hope it will be helpful :)

Community
  • 1
  • 1
ArloGrant
  • 249
  • 5
  • 15