Java using an enumeration with a switch statement

I looked at various Q & As on SO similar to this question, but could not find a solution.

I have an enumeration representing various ways to watch a TV guide ...

In the NDroid Application class

 static enum guideView { GUIDE_VIEW_SEVEN_DAY, GUIDE_VIEW_NOW_SHOWING, GUIDE_VIEW_ALL_TIMESLOTS } 

... when the user changes the view, the event handler gets an int from 0-2, and I would like to do something like this ...

In the Android Activity onClick(DialogInterface dialog, int which) event handler onClick(DialogInterface dialog, int which)

 // 'which' is an int from 0-2 switch (which) { case NDroid.guideView.GUIDE_VIEW_SEVEN_DAY: ... break; } 

I'm used to C # enumerations and select / case statements that would allow something like the above, and I know that Java does things differently, but I just can't figure out what I need to do.

Do I have to resort to if ? There will probably be only 3 options, so I could do it, but I was thinking how this can be done using a switch in Java.

EDIT Sorry, I didn’t fully solve this problem, because I saw it as a general problem with Java. I added to the question to explain a little further.

There is nothing special in Android, so I did not mark it as Android, but the enumeration was defined in the Application class, and the code in which I am disabled is in Activity . The enumeration is static, since I need to access it from several actions.

+78
java enums switch-statement
Nov 13 2018-11-11T00:
source share
7 answers

The part you are missing is converting from the whole to a safe name. Java will not do this automatically. There are several ways you can do this:

  • Use a list of static final integers, not an enumerated type, and include the resulting int value (this is an approach before Java 5)
  • Include either the specified id value (as described by heneryville ) or the ordinal value of the enumeration values; those. guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
  • Define the enumeration value represented by the int value, and then include the enumeration 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 a more useful / less error prone valueOf entry that takes your integer values ​​as an argument to resolve the corresponding enumeration value and allows you to centralize border checking.

+127
Nov 13 '11 at 2:10
source share

If whichView is an EnView GuideView object, then it works well. Please note that after case there is no qualifier for the constant.

 switch (whichView) { case SEVEN_DAY: ... break; case NOW_SHOWING: ... break; } 
+28
Feb 19 '13 at 3:34
source share

Enumerations should not be indicated on the case label, as you have NDroid.guideView.GUIDE_VIEW_SEVEN_DAY , instead you should remove the qualification and use GUIDE_VIEW_SEVEN_DAY

+8
Nov 13 '11 at 2:00
source share

This should work as you describe. What error are you getting? If you could use your code that would help.

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

EDIT: Are you sure you want to define a static enumeration? That sounds wrong to me. Enumeration is very similar to any other object. If your code compiles and runs, but gives incorrect results, this is likely to be the reason.

+2
Nov 13 2018-11-11T00:
source share
 enumerations accessing is very simple in switch case private TYPE currentView; //declaration of enum public enum TYPE { FIRST, SECOND, THIRD }; //handling in switch case switch (getCurrentView()) { case FIRST: break; case SECOND: break; case THIRD: break; } //getter and setter of the enum public void setCurrentView(TYPE currentView) { this.currentView = currentView; } public TYPE getCurrentView() { return currentView; } //usage of setting the enum setCurrentView(TYPE.FIRST); avoid the accessing of TYPE.FIRST.ordinal() it is not recommended always 
+1
Apr 26 '13 at 6:02
source share

I do it like

 public enum State { // Retrieving, // the MediaRetriever is retrieving music // Stopped, // media player is stopped and not prepared to play Preparing, // media player is preparing... Playing, // playback active (media player ready!). (but the media player // may actually be // paused in this state if we don't have audio focus. But we // stay in this state // so that we know we have to resume playback once we get // focus back) Paused; // playback paused (media player ready!) //public final static State[] vals = State.values();//copy the values(), calling values() clones the array }; public State getState() { return mState; } 

And use switch in statement

 switch (mService.getState()) { case Stopped: case Paused: playPause.setBackgroundResource(R.drawable.selplay); break; case Preparing: case Playing: playPause.setBackgroundResource(R.drawable.selpause); break; } 
0
May 22 '15 at 7:55
source share

An example of a short associative function:

 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 ""; } } 

As @Dhanushka said, omitting the qualifier inside the "switch" is the key.

0
Aug 01 '17 at 14:27
source share



All Articles