![](/img/trans.png)
[英]Delete button on list view only deletes the last item and not the particular one I want it to delete
[英]I want to delete an item from a list view
我遇到的问题是,无论何时删除,都只会删除ListView
的最后一项。 我从我的arraylist中删除该项目,然后调用adapter.notifyDataSetChanged()
。 有什么我想念的吗? 另外,当我尝试删除太多项目时,出现arrayIndexOutOfBounds
异常,但我正在传递我收到的索引。 为什么我收到此例外?
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
HistoryItem item = historyItems.get(index);
switch (index) {
case 0:
//ToDo edit
//Toast.makeText(History.this,"pos "+position+" index "+index,Toast.LENGTH_SHORT).show();
break;
case 1:
//ToDo delete
historyItems.remove(item);
adapter.notifyDataSetChanged();
您正在使用作为“编辑”或“删除”按钮位置的索引。必须使用“位置”而不是索引来从列表中删除项目。问题出在HistoryItem item = historyItems.get(index);
解:
HistoryItem item = historyItems.get(position);
不要使用index
使用position
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
HistoryItem item = historyItems.get(position);
switch (index) {
case 0:
//ToDo edit
//Toast.makeText(History.this,"pos "+position+" index "+index,Toast.LENGTH_SHORT).show();
break;
case 1:
//ToDo delete
historyItems.remove(position);
adapter.notifyDataSetChanged();
您正在使用索引变量删除项目。 您需要使用位置变量从历史记录列表中获取所选项目
在代码中更改此行
HistoryItem item = historyItems.get(position);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.