Java using enum with switch statement
Solution 1
The part you're missing is converting from the integer to the type-safe enum. Java will not do it automatically. There's a couple of ways you can go about this:
- Use a list of static final ints rather than a type-safe enum and switch on the int value you receive (this is the pre-Java 5 approach)
- Switch on either a specified id value (as described by heneryville) or the ordinal value of the enum values; i.e.
guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
-
Determine the enum value represented by the int value and then switch on the enum value.
enum GuideView { SEVEN_DAY, NOW_SHOWING, ALL_TIMESLOTS } // Working on the assumption that your int value is // the ordinal value of the items in your enum public void onClick(DialogInterface dialog, int which) { // do your own bounds checking GuideView whichView = GuideView.values()[which]; switch (whichView) { case SEVEN_DAY: ... break; case NOW_SHOWING: ... break; } }
You may find it more helpful / less error prone to write a custom
valueOf
implementation that takes your integer values as an argument to resolve the appropriate enum value and lets you centralize your bounds checking.
Solution 2
If whichView
is an object of the GuideView Enum, following works well. Please note that there is no qualifier for the constant after case
.
switch (whichView) {
case SEVEN_DAY:
...
break;
case NOW_SHOWING:
...
break;
}
Solution 3
The enums should not be qualified within the case label like what you have NDroid.guideView.GUIDE_VIEW_SEVEN_DAY
, instead you should remove the qualification and use GUIDE_VIEW_SEVEN_DAY
Solution 4
I like a few usages of Java enum:
- .name() allows you to fetch the enum name in String.
- .ordinal() allow you to get the integer value, 0-based.
- You can attach other value parameters with each enum.
- and, of course, switch enabled.
enum with value parameters:
enum StateEnum {
UNDEFINED_POLL ( 1 * 1000L, 4 * 1000L),
SUPPORT_POLL ( 1 * 1000L, 5 * 1000L),
FAST_POLL ( 2 * 1000L, 4 * 60 * 1000L),
NO_POLL ( 1 * 1000L, 6 * 1000L);
...
}
switch example:
private void queuePoll(StateEnum se) {
// debug print se.name() if needed
switch (se) {
case UNDEFINED_POLL:
...
break;
case SUPPORT_POLL:
...
break;
Solution 5
Short associative function example:
public String getIcon(TipoNotificacao tipo)
{
switch (tipo){
case Comentou : return "fa fa-comments";
case ConviteEnviou : return "icon-envelope";
case ConviteAceitou : return "fa fa-bolt";
default: return "";
}
}
Like @Dhanushka said, omit the qualifier inside "switch" is the key.
Squonk
Learned the fundamentals of BASIC programming in 1978 from a 4 page magazine pull-out. Since then I've coded in hex (6502, 8080, Z80, x86), Fortran, Pascal, C/C++, VB6, C# and I'm now hooked in to Java programming for Android devices. Over the years I've done most things from electronics design/development to furniture removals, factory work, fast food delivery and network systems administration. One day I'll work out what I want to do when I grow up. :-)
Updated on January 23, 2020Comments
-
Squonk over 4 years
I've looked at various Q&As on SO similar to this question but haven't found a solution.
What I have is an enum which represents different ways to view a TV Guide...
In the NDroid
Application
classstatic enum guideView { GUIDE_VIEW_SEVEN_DAY, GUIDE_VIEW_NOW_SHOWING, GUIDE_VIEW_ALL_TIMESLOTS }
...when the user changes the view an event handler receives an
int
from 0-2 and I'd like to do something like this...In an Android
Activity
onClick(DialogInterface dialog, int which)
event handler// 'which' is an int from 0-2 switch (which) { case NDroid.guideView.GUIDE_VIEW_SEVEN_DAY: ... break; }
I'm used to C# enums and select/case statements which would allow something like the above and I know Java does things differently but I just can't make sense of what I need to do.
Am I going to have to resort to
if
statements? There will likely only ever be 3 choices so I could do it but I wondered how it could be done with switch-case in Java.EDIT Sorry I didn't completely expand on the issue as I was looking at it as being a generic Java issue. I've added to the question to explain a bit further.
There isn't anything that's Android specific which is why I didn't tag it as Android but the enum is defined in the
Application
class and the code where I wan't the switch is in anActivity
. The enum is static as I need to access it from multiple Activities. -
trashgod over 12 years"Nested enum types are implicitly static. It is permissable to explicitly declare a nested enum type to be static."—JLS §8.9.
-
SystemParadox over 12 years@trashgod, indeed- but I would always use the implicit declaration to avoid confusion as that's actually more obvious IMO. A global static enum (which is what I assume this is) is probably wrong in most cases.
-
Squonk over 12 yearsMany thanks. It took me a while to work it into my code but it's working nicely now with the example code you posted. 6 years with C# and 1 year with Java - it's not often I come across something that stumps me. So many similarities but the occasional one like this which is so very different. I won't forget this one in a hurry. :-)
-
haridsv almost 10 yearsYou need the unqualified enum names in the case statement, so
case GuideView.SEVEN_DAY:
gives a compilation error, it should becase SEVEN_DAY:
. -
WORMSS almost 10 yearsThat only works if you are not getting your data externally. Since OP said which returns value 1, 2, 3 not a TYPE your method would not work without a reversed switch case that takes 1, 2, 3 and returns a TYPE before going into the switch case he listed..