开发者

onContextItemSelected never called using a Dialog with a ListView

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()));

registerForContextMe开发者_JAVA技巧nu(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.


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.


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) {


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜