onContextItemSelected 似乎没有被调用

发布于 2024-12-22 07:14:27 字数 1437 浏览 1 评论 0原文

我正在基于列表视图创建一个非常简单的活动。 我想为列表中的每一项添加上下文菜单,因此我调用了 registerForContextMenu(mListView)。 然后,我实现了方法 onCreateContextMenuonContextItemSelected

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

Oo萌小芽oO 2024-12-29 07:14:27

我在使用 ActionBarSherlock 库时遇到了同样的问题。

如果您正在使用该库,请确保导入正确的 MenuItem,即 android.view.MenuItem

或者只是在方法头中指定完整的包,如下所示:

public boolean onContextItemSelected(android.view.MenuItem item) {}

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:

public boolean onContextItemSelected(android.view.MenuItem item) {}
心房敞 2024-12-29 07:14:27

如果没有看到您的代码,我无法给出确切的答案,但您可以尝试手动添加 ContextMenuListener

getListView().setOnCreateContextMenuListener(this);

Without seeing your code, i can't give an exact answer, but you can try to add the ContextMenuListener manually

getListView().setOnCreateContextMenuListener(this);
一片旧的回忆 2024-12-29 07:14:27

删除此检查:

if (v == mListView) {
    ...
}

onCreateContextMenu(..) 方法

Remove this check:

if (v == mListView) {
    ...
}

from onCreateContextMenu(..) method

无可置疑 2024-12-29 07:14:27

我在这里找到了解决方法:
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 !

微暖i 2024-12-29 07:14:27

使用 OnMenuItemClickListener 就可以了。

Use OnMenuItemClickListener it is work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文