Why is item.getMenuInfo() null?

牧云@^-^@ 提交于 2019-12-02 06:41:51

You need to register the list for context menu, not the items.

// wrong:
activity.registerForContextMenu(vi);

// right:
activity.registerForContextMenu(myList);

It may not have anything to do with it, but one glaring thing I see right away is:

public Object getItem(int position) {
   return position;
}

public long getItemId(int position) {
   return position;
}

Your getItem method is returning an int of the object's position, not the actual Object itself as stated in its method declaration.

getMenuInfo() will work on ListAdapter, not on views.

But, You can pass additional data with the tag of the view.

in getView: vi.setTag(position) activity.registerForContextMenu(vi);

declare in Activity private int id;

onCreateContextMenu: id = (int) v.getTag();

onContextItemSelected: you can use the id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!