如何使用 ContextMenuItemSelected 长按从 ExpandableListView 中删除项目?
How to delete an item from ExpandableListView on long click with a ContextMenuItemSelected?
我正在尝试使用上下文菜单从 ExpandableListView 中删除一个项目。我运行陷入空指针异常。请帮我解决这个问题。下面是我的代码:
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
super.onContextItemSelected(item);
switch(item.getItemId())
{
case R.id.deleteItem:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
//this is my ArrayList
postList.remove(position);
Toast.makeText(getActivity().getApplicationContext(), "Deleted an item", Toast.LENGTH_LONG).show();
listAdapter.notifyDataSetChanged();
break;
case R.id.editItem:
break;
}
return true;
}
ContextMenuInfo 类型错误,Expandablelistview 中的项目有 2 个位置值,
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
我正在尝试使用上下文菜单从 ExpandableListView 中删除一个项目。我运行陷入空指针异常。请帮我解决这个问题。下面是我的代码:
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
super.onContextItemSelected(item);
switch(item.getItemId())
{
case R.id.deleteItem:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
//this is my ArrayList
postList.remove(position);
Toast.makeText(getActivity().getApplicationContext(), "Deleted an item", Toast.LENGTH_LONG).show();
listAdapter.notifyDataSetChanged();
break;
case R.id.editItem:
break;
}
return true;
}
ContextMenuInfo 类型错误,Expandablelistview 中的项目有 2 个位置值,
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);