Reputation: 415
Hey guys I was looking at the 'Diaro' and the 'my Diary' app available on the Android market.
The items get displayed in a list view, and on long clicking an item, a context menu with various options(like edit, delete etc ) open up. I tried implementing the same in my app which is some what similar. But the problem is in the onContextItemSelected(MenuItem item)
I can't get the contents of the item clicked. Here's the code for onContextItemSelected(MenuItem item)
:
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
break;v
// rest of the codetion
}
return super.onContextItemSelected(item);
}
Can somebody tell me as to how can I get the id of the item clicked on the list view from this function? I can really use some help here:)
Upvotes: 1
Views: 2634
Reputation: 3364
I had the same problem for a while and found out that I had onMenuitemselected() in my activity and this was listening to context menu item rather than on contextitemselected(), hope this helps.
Upvotes: 0
Reputation: 10639
You must register yourView for contextMenu, like this :
list = getListView();
registerForContextMenu(list);
and you must use onCreateContextMenu for build it
@Override
public void onCreateContextMenu(ContextMenu contextMenu,
View v,
ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menuInfo;
selectedWord = ((TextView) info.targetView).getText().toString();
selectedWordId = info.id;
contextMenu.setHeaderTitle(selectedWord);
contextMenu.add(0, CONTEXT_MENU_EDIT_ITEM, 0, R.string.edit);
contextMenu.add(0, CONTEXT_MENU_DELETE_ITEM, 1, R.string.delete);
}
you have the listView item in your contextMenu title and it's id in selectedWordId
for more see this link : Detecting which selected item (in a ListView) spawned the ContextMenu (Android)
Upvotes: 3