How to remove the selected item from the list?

I would like to know how to remove an item (which the user can select) from the ListView in the user interface. ListView contains only TextViews that show the IP address. Now, when I click the (Delete) button, I want to remove the selected item from the ListView.

Now I track the selected items using the ArrayList array, which stores the item indices. I set the SelectionMode ListView parameter to multipleChoice, so these indices must be accurate. I don’t know how to do this, but my way looks like this:

mEndPointList.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){ boolean found = false; int index = 0; while(!found && index >= 0 && index < mSelectedItems.size()){ if(mSelectedItems.get(index).intValue() == arg2){ found = true; } index++; } if(!found){ mSelectedItems.add(new Integer(arg2)); } } }); 

Now, when I finish the selection of elements, I click the "Delete" button, which should delete the elements in the saved indexes. The code from the button is as follows:

 public class RemoveItemButtonHandler implements OnClickListener{ @Override public void onClick(View v){ for(int index = 0; index < mSelectedItems.size(); index++){ int selectedItemIndex = mSelectedItems.get(index); mEndPointList.removeViews(selectedItemIndex, 1); } mSelectedItems.clear(); mEndPointList.postInvalidate(); } } 

This code is added as the onClickListener button for the Delete button. The code will execute without any problems, but the item will not be removed from the ListView. Does anyone have an idea why it is not working?


I think it’s fair to show my decision if someone else wonders how I actually did it. The selection is made in the OnItemClickListener list:

 mEndPointList = ((ListView) findViewById(R.id.endpointList)); mEndPointList.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){ boolean found = false; int index = 0; /* Loop through all current selected item indexes to see if * there is a match. If not, add the index to the list of * selected indexes. If the index is already present, remove * the index from the list. */ while(!found && index >= 0 && index < mSelectedItems.size()){ if(mSelectedItems.get(index).intValue() == arg2){ found = true; mSelectedItems.remove(index); } index++; } if(!found){ mSelectedItems.add(new Integer(arg2)); } mSelectedItems.trimToSize(); } }); 

Elements are deleted as follows:

 public class RemoveItemButtonHandler implements OnClickListener{ @Override public void onClick(View v){ // Check to see if any items are selected. if(mSelectedItems.size() == 0){ String message = "No items selected.\nTo select, press once on item.\n" + "To unselect, press item again."; Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); return; } // Sort the selected item indexes from high to low to prevent List corruption. Collections.sort(mSelectedItems, new DescendingComparator()); /* Iterate through the selected items and remove items from the EndPoint List * using the selected item index. Corruption of the List is prevented by * sorting the selected items list from high to low, thus the item with the * highest index is removed first. */ if(mRawEndPoints.size() > 1){ for(int index = 0; index < mSelectedItems.size(); index++){ int selectedItemIndex = mSelectedItems.get(index); mRawEndPoints.remove(mListAdapter.getItem(selectedItemIndex)); } // Update the Adapter to notify it data has changed. mListAdapter.notifyDataSetChanged(); } else { String message = "Cannot remove last item from the list."; Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); } // Clear the List of selected items for a fresh selection. mSelectedItems.clear(); } } 

Note that the DescendingComparator class is a custom class that implements the Comparator<Integer> interface.

+4
source share
1 answer

Instead of calling the postInvalidate() method, call the notifyDataSetChanged the list adapter.


Oh wait ... I'm just re-reading your code. You are trying to remove views from the list: S You should never do this. A ListView is just a widget to display data; this data is supported by the adapter where the actual data is . In your case, what you would like to do is remove the elements from the array, and then notify you of this change (using an adapter, which will eventually lead to an update of the ListView ).

+4
source

Source: https://habr.com/ru/post/1339670/


All Articles