0

I have an array string that i used in my Fragment,and i show the array string items with setListAdapter in my list:

  public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
 String[] array = getResources().getStringArray(R.array.examlearray);
        final ArrayList<String> str = new ArrayList<String>(Arrays.asList(array));
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, str );
        setListAdapter(arrayAdapter);

  final ListView listView = getListView();       
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {...

and under my onActionItemClicked i want to implement my deleteSelectedItem() method,that delete selected list items,and this my code,but it didn't remove selected item,it is just remove from first of list,and when i select all the items and press remove,the app crash!what should do?,Any help would be appreciated! Thanks!

 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // Respond to clicks on the actions in the CAB
                switch (item.getItemId()) {
                    case R.id.delete:
                    //    deleteSelectedItems();

                         Log.i(TAG, "deleteSelectedEntries");
                         SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
for(int i=0;i<checkedItems.size();++i)

                         { if(checkedItems.valueAt(i))
                             str.remove(i);

                         }
                         arrayAdapter.notifyDataSetChanged();
Ladan Nekuii
  • 185
  • 1
  • 6
  • 18
  • Visit that answer and make custom listview http://stackoverflow.com/questions/23485986/custom-adapter-for-a-list-of-items-that-have-multiple-child-items/23486051#23486051 – Engr Waseem Arain Jun 18 '14 at 06:26

3 Answers3

1

Instead of looping through the SparseBooleanArray you get from listView.getCheckedItemPositions(), you have to loop through the items of the ArrayAdapter and then check if the SparseBooleanArray returns true for this item. This is because when you remove items from the ArrayAdapter, listView.getCheckedItemPositions() still returns items that don't exist anymore.

So in your case:

SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
// Loop backwards, so you can remove the items from within the loop
for (int i = arrayAdapter.getCount() - 1; i >= 0; i--) {
    if (checkedItems.get(i)) {
        // This item is checked and can be removed
        arrayAdapter.remove(arrayAdapter.getItem(i));
    }
}

The reason your app crashed is because you try to remove non existing items.

michielve
  • 514
  • 4
  • 16
0

You will need to hold a reference to your array adapter. Then try calling the remove function on the adapter followed by notifyDataSetChanged. If you only have a reference to the position, you will also need to use getItem.

Chris Feist
  • 1,678
  • 15
  • 17
0

Keep the reference of adapter and data list used for creating adapter, Its better to use ArrayList, since it gives flexibility to remove elements easily. To set the adapter you can use following code -

String[] array = getResources().getStringArray(R.array.examlearray);
ArrayList<String> str = new ArrayList<String>(Arrays.asList(array));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, str );
menuListView.setAdapter(arrayAdapter);

To remove element first remove the element from the data list and then calling notifyDatasetChnaged will update the listView as well. you can call following code inside onActionItemClicked

`for(int i=0;i<checkedItems.size();++i) 
      {  if(checkedItems.valueAt(i))
              str.remove(i);
      }

arrayAdapter.notifyDataSetChanged();
Pr38y
  • 1,565
  • 13
  • 21
  • tanks,adapter works well but my remove didn't work,this is my code: SparseBooleanArray checkedItems = listView.getCheckedItemPositions(); for(int i=0;i – Ladan Nekuii Jun 18 '14 at 08:27
  • Your `SparseBooleanArray` will contain array of boolean corresponding to each position, if it is true item is checked otherwise it is unchecked. so remove the item when it is checked and inside remove you have to pass the position. I have edited the answer check now. – Pr38y Jun 18 '14 at 08:52
  • if I select 3 items,it's removing 3 first items !,not the selected items,and if I select all the items an press remove,the app will crash,what should I do? – Ladan Nekuii Jun 18 '14 at 11:17
  • can you debug and see the values in checkedItems array? Are you setting choice mode `CHOICE_MODE_NONE` for your listview? – Pr38y Jun 19 '14 at 04:54