3

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.

Leb
  • 15,483
  • 10
  • 56
  • 75
ErGo_404
  • 1,801
  • 4
  • 18
  • 22

5 Answers5

15

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) {}
jenzz
  • 7,239
  • 6
  • 49
  • 69
0

Use OnMenuItemClickListener it is work.

0

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

getListView().setOnCreateContextMenuListener(this);
poitroae
  • 21,129
  • 10
  • 63
  • 81
  • I added the code. The thing is my menu does show up and the onCreateContextMenuListener is called. However I still wanted to try to do it manually (with your method) and it did not work either. – ErGo_404 Dec 22 '11 at 16:18
0

Remove this check:

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

from onCreateContextMenu(..) method

a.ch.
  • 8,285
  • 5
  • 40
  • 53
  • In my real code I add a log in the "else" of this if so I would now if it did not enter the menu creation part code. But again, the menu is correctly created as it shows up. – ErGo_404 Dec 22 '11 at 16:35
  • Sorry, didn't notice that in your question. What happens if you comment this part of code: `mListView.setOnItemClickListener(new OnItemClickListener() { ... };`? – a.ch. Dec 22 '11 at 16:45
0

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 !

Community
  • 1
  • 1
ErGo_404
  • 1,801
  • 4
  • 18
  • 22