Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从listview中删除选定项?_Java_Android - Fatal编程技术网

Java 如何从listview中删除选定项?

Java 如何从listview中删除选定项?,java,android,Java,Android,我想知道如何删除用户可以从用户界面的ListView中选择的项目。 ListView仅包含显示IP地址的文本视图。现在,当我按下“删除”按钮时,我想从ListView中删除所选项目 现在,我通过使用保存项目索引的ArrayList来跟踪所选项目。我已经将ListView的choiceMode设置为multipleEchoice,因此这些索引应该是准确的。 我不知道最好的方法,但我的方法如下: mEndPointList.setOnItemClickListener(new OnItemClick

我想知道如何删除用户可以从用户界面的ListView中选择的项目。 ListView仅包含显示IP地址的文本视图。现在,当我按下“删除”按钮时,我想从ListView中删除所选项目

现在,我通过使用保存项目索引的ArrayList来跟踪所选项目。我已经将ListView的choiceMode设置为multipleEchoice,因此这些索引应该是准确的。 我不知道最好的方法,但我的方法如下:

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));
        }
    }
});
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();
    }
}
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();
    }
}
移除项目的操作如下所示:

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));
        }
    }
});
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();
    }
}
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();
    }
}
请注意,DegeneratingComparator类是一个自定义类,它实现了Comparator接口。

调用列表适配器的notifyDataSetChanged,而不是调用列表的postInvalidate方法


哦,等等。。。我只是重读了你的代码。您正试图从列表中删除视图:S您决不能这样做。ListView只是一个显示数据的小部件;该数据由适配器支持,适配器是数据实际所在的位置。在您的情况下,您需要做的是从阵列中删除项,然后使用适配器通知更改;这最终将导致列表视图被更新。

对不起,我不是有意添加另一条评论。。。但是当我试图通过ListAdapter=mEndPointList.getAdapter;获取它时;,它不会让我改变。我猜这是从扩展ListActivity而不是Activity开始的函数,但我不想这样做……您是如何填充列表的?你一定用过适配器,对吗?粘贴该部件的代码。通常不需要使用getAdapter方法,而是使用用于填充列表视图的适配器对象;EndpointList.setAdapternew ArrayAdapterthis,R.layout.list_项,rawEndPoints;我已经尝试了你发布的内容,但它不允许我调用notifyDataSetChanged。而且,为了让事情更简单,不要匿名使用适配器。创建一个字段变量以保存适配器的实例,以便轻松使用notifyDataSetChanged。