Android Java: using enums

15,481

Solution 1

You can add a method as below in your mTab enum -

public static mTab toMTab(int val) {
    mTab retMTab = null;
    for (mTab tempTab : mTab.values()) {
        if(tempTab.getValue() == val)  {
            retMTab = tempTab;
            break;
        }
    }
    return retMTab;
}

and then in your onPrepareOptionsMenu() you can do like this 0

switch(mTab.toMTab(intVal)) {
  case TAB_NAME:
     ...
     break;
  case TAB_INGREDIENT:
     ..
     ...

}

Solution 2

Enum variables can be placed directly in switch statements in Java. I am however concerned that enum may not be the most appropriate option in this case because each of your tabs is doing something different: decorating or subclassing imho would be better. From what I've learnt from Josh Bloch, if you have to add methods to an enum, they should all be the same name, for example: operations on a calculator.

Each letter being part of the enum:

Menu v;
...
    switch (v)
    {
        case A:
           ...
        case B:
        default:
    }
Share:
15,481
Tianxiang Xiong
Author by

Tianxiang Xiong

Business Intelligence Developer at Epic in Verona, WI. Interested in Lisp, Android, and technology in general.

Updated on June 14, 2022

Comments

  • Tianxiang Xiong
    Tianxiang Xiong almost 2 years

    I'm wondering how to use enums in Java. I'm working on a recipe application for Android. I have a screen with four tabs in a TabHost and would like to refer to them through named constants, and I believe that it's best to do it through an enum.

    private enum mTab {
        TAB_NAME(0), TAB_INGREDIENT(1), TAB_STEP(2), TAB_MEDIA(3);
    
        final int numTab;
    
        private mTab(int num){
            this.numTab = num;
        }
    
        public int getValue(){
            return this.numTab;
        }
    
    };
    

    Now I'd like to create a different menu for each tab. For example, for TAB_INGREDIENT I'd like a menu option called "Add ingredient", while for TAB_MEDIA I'd like a menu option called "Add image".

    I'm creating the menus through an onPrepareOptionsMenu(), like so

    public boolean onPrepareOptionsMenu(Menu menu) {
        // Clear menu before showing new menu
        menu.clear();
    
        super.onPrepareOptionsMenu(menu);
    
        // Create new menu based on current tab
        MenuInflater inflater = getMenuInflater();
        int tab = getTabHost().getCurrentTab();
    
        switch (tab) {
            ...
                }
    
        return true;
    }
    

    The problem is that I don't know what to put in the switch statement. More specifically, I don't know how to compare "tab", which is an integer corresponding to the currently selected tab, to an enum element.

  • Tianxiang Xiong
    Tianxiang Xiong over 12 years
    Thanks Gopi, that seems to work. So basically I'd convert the int value of the current tab to an mTab through a switch statement. I think this also means that I don't need the mTab constructor, getValue(), etc. anymore, at least for this purpose?
  • Gopi
    Gopi over 12 years
    yeah you may do away with getValue() and use the ordinal() method already available with every enum.