Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onOptionsItemSelected return incorrect ID

Tags:

android

menu

(Newbe)

When I click on a menu the above method returns an ID from the first menu, not the one I clicked. If I check for the Title Condensed of the menu it is correct.

int id = item.getItemId();   //returns id of an incorrect menu
String Title = (String) item.getTitleCondensed();  //this returns the correct title.

Any ideas welcome.

like image 839
Olan Avatar asked Aug 19 '11 19:08

Olan


2 Answers

I had the same problem. Generated files from the build are not properly updated.

I got the same effect if i reordered the menu items in the xml...build and surprise. Clicking on menu brings other codes than expected.

Do a clean and try again

like image 80
mhstnsc Avatar answered Oct 10 '22 12:10

mhstnsc


You should have set each menu item a unique ID in onCreateOptionsMenu and onCreateContextMenu.

For example:

    public static final int CONTEXT_MENU_DELETE = Menu.FIRST;
    public static final int CONTEXT_MENU_EDIT = CONTEXT_MENU_DELETE + 1;

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, 
                    ContextMenuInfo menuInfo) {

            menu.add(0, CONTEXT_MENU_DELETE, 1, R.string.delete);
            menu.add(0, CONTEXT_MENU_EDIT, 2, R.string.edit);
    }

    // And then

    @Override
    public boolean onContextItemSelected(MenuItem item) {

            switch(item.getItemId()) {

            case CONTEXT_MENU_DELETE:
                    // Delete item
                    break;

            case CONTEXT_MENU_EDIT:
                    // Edit item
                    break;
            }
    }

The same is for onCreateOptionsMenu and onOptionsItemSelected. You should have a unique constant for every menu option.

Added:

Didn't you check out this tutorial? The idea is the same. You should set different ids in menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/new_game"
              android:icon="@drawable/ic_new_game"
              android:title="@string/new_game" />
        <item android:id="@+id/help"
              android:icon="@drawable/ic_help"
              android:title="@string/help" />
    </menu>

And then use those ids in onOptionsItemSelected:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.new_game:
            newGame();
            return true;
        case R.id.help:
            showHelp();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

Check out these code blocks from official Android Menu tutorial and compare to your own code. You could also publish your menu.xml, onCreateOptionsMenu and onOptionsItemSelected so it would be easy to figure out your problem.

like image 40
Victor Avatar answered Oct 10 '22 13:10

Victor