0

I'm trying to create a android menu which should be build dynamically inside a Google Glass app. Therefore I have to arrays which contain the diffent kinds of objects which should be displayed in the menu. The menu should look like the following:

  • Menu1
    • Option1
    • Option2
    • Option3
  • Menu2
    • Option1
    • Option2
  • Menu3
  • Menu4

I've already build up the menu structure with this code:

public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();
    SubMenu damageCodes = menu.addSubMenu(R.string.chooseDamageCode).setIcon(R.drawable.ic_document_50);
    int i = 0;
    for(Damagecode d : this.mDamagecodes){
        damageCodes.add(0, Menu.NONE, i, d.getCotext());
        i++;
    }
    SubMenu priorities = menu.addSubMenu(R.string.choosePriority).setIcon(R.drawable.ic_document_50);
    i = 0;
    for(Priority p : this.mPriorities){
        priorities.add(1, Menu.NONE, i, p.getPriokx());
        i++;
    }
    menu.add(3, Menu.NONE, Menu.NONE, R.string.setConfirmationText).setIcon(R.drawable.ic_pen_50);
    menu.add(4, Menu.NONE, Menu.NONE, R.string.backToTplnr_equipment).setIcon(R.drawable.ic_reply_50);
    getMenuInflater().inflate(R.menu.create_notification, menu);
    return true;
}

I know that the method

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
}

is called when a menuitem is selected but the question right now is how to get the selected item?

user2858559
  • 39
  • 1
  • 9

2 Answers2

0

Just put a switch inside the onMenuItemSelected:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    switch(item.getTitle()) {
    case R.string.chooseDamageCode:
        // do stuff here for damagecode item
    break;
    case R.string.choosePriority:
        // do stuff here for choosepriority item
    break;

    ...same for other items
    }
}

Hope this helps

eclipse1203
  • 535
  • 4
  • 14
  • hey, looks easy but actuall doesn't work :) due to the fact that I don't know which values can be in my array I can't ask with a simple switch-case statement. I found a solution which you can find as a new answer! But anyway, thx a lot for your answer :) – user2858559 Nov 10 '14 at 08:55
0

I found a solution. Due to the fact that I don't know which elements / values can be in my array I created a simple flag. This means... I'm creating the main menu where every item has a fixed unique ID. The submenu elements do not have a unique id, they only have the title. So what I'm doing right now is to check first whether one of the main menu items was pressed (unique ID) or whether the title of the clicked element is either in one of the arrays or not. Works quite fine :) Hope this helps anybody else too!

Greetings

user2858559
  • 39
  • 1
  • 9