4

I'm creating a simple dialog with a ListView on it. I want to be able to access a context menu on it. Here's the basic code I've:

<On CreateDialog>
listViewSongs=(ListView) layout.findViewById(R.id.ListView_Songs);
listViewSongs.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, drawingPanel.metronome.getSongNames()));

registerForContextMenu(listViewSongs);  

Then I just add a simple item:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);
 menu.setHeaderTitle("Sample Context Menu");
    menu.add(0, MENU_EDIT_SONG, 0, "Edit");
}

And finally I override the onContextItemSelected:

@Override
public boolean onContextItemSelected(MenuItem item) {
 super.onContextItemSelected(item);
    editSong();
    return true; 
}

So my problem is that when I longpress the listview I got the context menu, but after clicking the only option on it, it never calls onContextItemSelected :( Any help?

PS: I've tried also to override onMenuItemSelected, onOptionsItemSelected, but I got the same result :\ never got called.

fernandojsg
  • 1,090
  • 18
  • 27
  • It sounds like you want an Activity and not a Dialog. – Falmarri Oct 25 '10 at 17:41
  • why are you calling super.onOptionsItemSelected(item); from onContextItemSelected? How did you determine that onContextItemSelected is never being called? Break point? logging? Did you look in logcat for any errors? – Cheryl Simon Oct 25 '10 at 17:42
  • @Mayra: sorry It was a typo, It's not super.onOptionsItemSelected but onContextItemSelected, in the code it was good. I just edit it. And I got that never called because I put breakpoints just entering the function. – fernandojsg Oct 25 '10 at 17:48
  • @Falmarri: Why? The dialog do the job quite good for what I need. It's any limitation on using contextmenus on dialogs? – fernandojsg Oct 25 '10 at 17:49

3 Answers3

6

I'm not sure why what you have isn't working, but you could try adding a listener to your menuItem instead: setOnMenuItemClickListener. At least that would tell you that your context menu item was being selected.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • It works using that listener, even if I see also a little bit confusing why the code is not working, i'll take the way you suggest till I'll investigate a little bit more for other solution. Thank u – fernandojsg Oct 25 '10 at 18:11
  • Thank you very much Cheryl Simon! – pedro.olimpio Mar 22 '15 at 15:07
0

You must put YOUR CODE before the calling the base method (i.e., super())

0

After this line:

    registerForContextMenu(listViewSongs); 

type this:

    listViewSongs.setOnCreateContextMenuListener(this);

And instead of onContextItemSelected(MenuItem item) function use this:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem menuItem) {
Miki
  • 440
  • 4
  • 7
  • This didn't work in DialogFragment - using layout inflater to create the menu from XMl. Had to do Mayra's answer and create items individually and set onMenuItemClicklistener on each item – Aiden Fry Mar 06 '13 at 11:07