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.