[英]Delete item in the list view using Context Menu
我想使用上下文菜单从列表视图中删除一个条目。 这是我的代码:
@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);
}
}
在这里,info.position会提取正确的list_record,但是当我在info.position中使用相同的内容时,所选项目正下方的项目将被删除。 我该怎么办?
这应该工作:
//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.