onContextItemSelected 似乎没有被调用
我正在基于列表视图创建一个非常简单的活动。 我想为列表中的每一项添加上下文菜单,因此我调用了 registerForContextMenu(mListView)
。 然后,我实现了方法 onCreateContextMenu
和 onContextItemSelected
。
onCreateContextMenu
可以工作(上下文菜单正确显示),但是当我单击此菜单的某个项目时,什么也没有发生,上下文菜单只是消失,并且不调用方法 onContextItemSelected
(我只是在里面放了一个日志来检查)。
如果有帮助,请注意 ListView
还附加了一个 onItemClickListener
。
我是不是忘记了什么?
谢谢 !
编辑:这是代码(我隐藏了一些不相关的东西)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView = new ListView(this);
mContacts = new Vector<Contact>();
mAdapter = new ContactAdapter(this, mContacts);
registerForContextMenu(mListView);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
... };
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v == mListView) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_item_contextmenu, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Log.v("Contacts", "onContextItemSelected called");
return super.onContextItemSelected(item);
}
编辑2:我添加了 onContextMenuClosed()
方法,该方法在菜单关闭时正确调用。
I am creating a very simple Activity based on a list view.
I want to add a context menu to each one of the items in the list, so I called registerForContextMenu(mListView)
.
I then implemented the methods onCreateContextMenu
and onContextItemSelected
.
The onCreateContextMenu
works (the context menu appears correctly), but when I click on an item of this menu nothing happens, the context menu just disappears and the method onContextItemSelected
is not called (I just put a log inside it to check).
If it can help, note that the ListView
also has a onItemClickListener
attached to it.
Did I forget something ?
Thanks !
EDIT: here's the code (I hid some irrelevant stuff)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView = new ListView(this);
mContacts = new Vector<Contact>();
mAdapter = new ContactAdapter(this, mContacts);
registerForContextMenu(mListView);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
... };
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v == mListView) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_item_contextmenu, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Log.v("Contacts", "onContextItemSelected called");
return super.onContextItemSelected(item);
}
EDIT 2: I added the onContextMenuClosed()
method, which is properly called when the menu is closed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在使用 ActionBarSherlock 库时遇到了同样的问题。
如果您正在使用该库,请确保导入正确的 MenuItem,即
android.view.MenuItem
。或者只是在方法头中指定完整的包,如下所示:
I have experienced the same problem using the ActionBarSherlock library.
If you are using that library, make sure you import the correct MenuItem which is
android.view.MenuItem
.Or just specify the full package in the method header like this:
如果没有看到您的代码,我无法给出确切的答案,但您可以尝试手动添加 ContextMenuListener
Without seeing your code, i can't give an exact answer, but you can try to add the ContextMenuListener manually
删除此检查:
从
onCreateContextMenu(..)
方法Remove this check:
from
onCreateContextMenu(..)
method我在这里找到了解决方法:
onContextItemSelected 从未使用带有 ListView 的对话框调用
但是,我还是想知道为什么它不能按预期工作。
感谢大家抽出宝贵的时间!
I found a workaround here :
onContextItemSelected never called using a Dialog with a ListView
However, I would still like to know why it does not work as expected.
Thank you all for your time !
使用 OnMenuItemClickListener 就可以了。
Use OnMenuItemClickListener it is work.