我想知道如何从用户界面的 ListView 中删除一个项目(用户可以选择)。 ListView 仅包含显示 IP 地址的 TextView。现在,当我按下按钮(删除)时,我想从 ListView 中删除所选项目。
现在我通过使用 ArrayList 来跟踪选定的项目,它包含项目的索引。我已经将 ListView 的 choiceMode 设置为 multipleChoice,所以这些索引应该是准确的。 我不知道最好的方法,但我的方法是这样的:
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();
}
}
此代码添加为“删除”按钮的 onClickListener。 代码将毫无问题地执行,但项目不会从 ListView 中删除。 有人知道为什么它不起作用吗?
我想展示我的解决方案是公平的,以防其他人想知道我是如何做到的。
选择在 OnItemClickListener
中完成列表的:
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();
}
});
删除项目是这样完成的:
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();
}
}
请注意 DescendingComparator
class 是一个自定义类,它实现了 Comparator<Integer>
界面。
最佳答案
不是调用列表的 postInvalidate()
方法,而是调用列表适配器的 notifyDataSetChanged
。
哦等等...我刚刚重新阅读了您的代码。您正试图从列表中删除 View :S 您永远不能这样做。 ListView
只是一个显示数据的小部件;该数据由一个适配器支持,该适配器实际上是数据所在的位置。在你的情况下,你想要做的是从数组中删除项目,然后通知该更改(使用适配器;这最终将导致 ListView
更新)。
关于java - 如何从 ListView 中删除选定的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4993523/