AdapterContextMenuInfo总是空 [英] AdapterContextMenuInfo is always null
问题描述
我试图通过这本书从Android开发文档这样做
I tried doing this by the book from android dev docs:
// this didn't create a menu, i don't know why
//registerForContextMenu(getListView());
setListAdapter(new ArrayAdapter<Note>(this, R.layout.selectset_listitem) {
@Override
protected View getView(...) {
... custom layout ...
// this creates a menu, but...
registerForContextMenu(convertView);
return convertView;
}
}
以及 onCreateContextMenu
和 onContextItemSelected
几乎完全一样的的 http://developer.android.com/guide/topics/ui/menus.html#context-menu
在这里是如何看起来在文档(和我的code):
here is how it looks in the docs (and my code):
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
但这一部分总是给我一个空的信息:
but this part always gives me a null info:
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
...
}
这是真正独特的唯一的事情是,我已经有了一个列表项自定义布局(即一对夫妇的文本字段和图像)。有什么我需要做的是能够获取列表项,上下文菜单是为?建的指数
The only thing that is really unique is that i've got a custom layout for list items (i.e. a couple of text fields and an image). Is there something i need to do to be able to get the index of the list item that the context menu was built for?
推荐答案
您需要调用 registerForContextMenu()
中的的ListView活动
,而不是在适配器的视图中的项目。
You need to call the registerForContextMenu()
in the activity on the ListView
, and not on the view items in the adapter.
这篇关于AdapterContextMenuInfo总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!