[英]How to remove a selected item from a listview?
我想知道如何從用戶界面上的ListView中刪除項目(用戶可以選擇)。 ListView僅保存顯示IP地址的TextView。 現在,當我按下一個按鈕(刪除)時,我想從ListView中刪除所選項目。
現在,我通過使用ArrayList跟蹤所選項目,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添加。 代碼將執行沒有任何問題,但Item不會從ListView中刪除。 有沒有人知道它為什么不起作用?
我想只有在其他人想知道我是如何實現它的情況下展示我的解決方案才是公平的。 選擇在List的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
類是一個自定義類,它實現Comparator<Integer>
接口。
而不是調用列表的postInvalidate()
方法,而是調用列表適配器的notifyDataSetChanged
。
哦等等......我只是重新閱讀你的代碼。 您正試圖從列表中刪除視圖:S您不能這樣做。 ListView
只是一個顯示數據的小部件; 該數據由適配器支持, 該適配器實際上是數據 。 在您的情況下,您要做的是從數組中刪除項目,然后通知該更改(使用適配器;最終將導致更新ListView
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.