[英]Delete item in the list view using Context Menu
I want to use the context menu to delete an entry from the list view. 我想使用上下文菜单从列表视图中删除一个条目。 This is my code:
这是我的代码:
@Override
public boolean onContextItemSelected(MenuItem item) {
// Log.d("", "logger enter onContextItemSelected");
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case 1:
int i = (int) info.id;
String list_record = separated.get(info.position);
Utils.removeLabels(this, list_record);
Log.d("", "labels>>> position: " + info.position);
separated.remove(info.position);
Log.d("", "labels>>> separated=" + separated);
clAdapter.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
Here, info.position extracts the correct list_record but when I use the same thing in info.position, the item just below the selected item gets deleted. 在这里,info.position会提取正确的list_record,但是当我在info.position中使用相同的内容时,所选项目正下方的项目将被删除。 How do I go about this?
我该怎么办?
This should work: 这应该工作:
//This is just an hack
int id = info.position == 0 ? 0 : info.position -1;
clAdapter.remove(clAdapter.getItem(id));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.