开发者

How to remove a selected item from a listview?

I would like to know how to remove an item (which a user can select) from a ListView on the User Interface. The ListView holds only TextViews which show an IP-Address. Now when I press a button (Remove) I want to remove the selected item from the ListView.

Now I keep track of the selected items through the use of an ArrayList, which hold the indexes of the items. I have set the choiceMode of the ListView to multipleChoice, so these indexes should be accurate. I don't know the best way to do it, 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 am finished selecting the Items, I press the Remove button, which should remove the items at the stored Indexes. The code from the button looks like this:

public class RemoveItemButtonHandler implements OnClickListener{
    @Override
    public void onClick(View v){
        for(int index = 0; index < mSelectedItems.size(); i开发者_如何学运维ndex++){
            int selectedItemIndex = mSelectedItems.get(index);
            mEndPointList.removeViews(selectedItemIndex, 1);
        }

        mSelectedItems.clear();
        mEndPointList.postInvalidate();
    }
}

This code is added as the onClickListener for the Remove button. The code will execute without any problems, but the Item doesn't get removed from the ListView. Does anybody have an idea on why it doesn't work?


I guess it is only fair to show my solution in case anybody else wonders how I actually did it. Selection is done in the OnItemClickListener of the 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();
    }
});

Removal of the items is done like this:

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's 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, which implements the Comparator<Integer> interface.


Instead of invoking the postInvalidate() method of the list, invoke the notifyDataSetChanged of the list's adapter.


Oh wait... I just re-read your code. You are trying to remove the views from the list :S You must not do that ever. A ListView is just a widget to display data; that data is backed by an adapter which is where the data actually is. In your case, what you would want to do is remove the items from the array and then notify that change (using the adapter; which finally will cause the ListView to be updated).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜