Reputation: 325
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 items from my list that selected,and this is my code,but it didn't remove selected item,it is just remove from first of list,and where ever it wants!!!what should I do?,Any help would be appreciated! Thanks!
@Override
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(listView.getItemAtPosition(i));
}
}
arrayAdapter.notifyDataSetChanged();
mode.finish();
return true;
}
Upvotes: 0
Views: 929
Reputation: 11948
your code in many situation returned ArrayIndexOutOfBound
.
one example:
you have a list with 14 element, you select first and last index on it. so now in for statement you go trough your list and delete from that:
in this scenario you delete first index of your list, so size of your list became 13, then for remove last item you have tries to access 14th element of your list that is not exists
How to solve:
for(int i=checkedItems.size()-1 ;i>= 0;i--){
if(checkedItems.valueAt(i)){
str.remove(checkedItems.keyAt(i));
}
}
Upvotes: 0
Reputation: 1171
The key of the SparseBooleanArray
denotes the position of the selected item in the listview. So instead of str.remove(listView.getItemAtPosition(i));
use str.remove(checkedItems.keyAt(i));
@Override
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(checkedItems.keyAt(i));
}
}
arrayAdapter.notifyDataSetChanged();
mode.finish();
return true;
}
Upvotes: 1
Reputation: 3504
in your code,you are deleting from the final ArrayList str(why declare it final if you plan to remove/change data?) which is present in your activity.
you need to use
arrayAdapter.remove(listView.getItemAtPosition(i))
then
arrayAdapter.notifyDataSetChanged();
Upvotes: 0