Java using enum with switch statement

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 class

static 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 an Activity. The enum is static as I need to access it from multiple Activities.

356360 次浏览

这应该按照你描述的方式工作。你得到了什么错误?如果你能粘贴你的代码,那会有帮助。

Http://download.oracle.com/javase/tutorial/java/javaoo/enum.html

编辑: 确实要定义静态枚举吗?我觉得不太对劲。枚举与其他对象非常相似。如果您的代码编译并运行但给出不正确的结果,这可能就是原因所在。

枚举不应该像 NDroid.guideView.GUIDE_VIEW_SEVEN_DAY那样限定在 case 标签中,而是应该删除限定并使用 GUIDE_VIEW_SEVEN_DAY

缺少的部分是将整数转换为类型安全枚举。Java 不会自动完成。有几种方法可以做到这一点:

  1. 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)
  2. 打开指定的 id 值(如 被 Heneryville 描述过)或枚举值的序号值(即 guideView.GUIDE_VIEW_SEVEN_DAY.ordinal())
  3. 确定由 int 值表示的枚举值,然后打开枚举值。

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

    您可能会发现编写一个自定义 valueOf实现更有帮助/更少的错误,该实现将整数值作为参数来解析适当的枚举值,并允许您集中进行边界检查。

如果 whichView是 GuideViewEnum 的对象,则以下操作可以正常工作。请注意,在 case之后没有常量的限定符。

switch (whichView) {
case SEVEN_DAY:
...
break;
case NOW_SHOWING:
...
break;
}
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

我就是这么做的

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

并在 Switch 语句中使用

switch (mService.getState())
{
case Stopped:
case Paused:


playPause.setBackgroundResource(R.drawable.selplay);
break;


case Preparing:
case Playing:


playPause.setBackgroundResource(R.drawable.selpause);
break;
}

简短的联想函数例子:

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.

I like a few usages of Java enum:

  1. Name ()允许您获取字符串中的枚举名称。
  2. . ordinal ()允许您获取从0开始的整数值。
  3. 可以为每个枚举附加其他值参数。
  4. 当然,还启用了开关。

具有值参数的枚举:

    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);
...
}

切换例子:

private void queuePoll(StateEnum se) {
// debug print se.name() if needed
switch (se) {
case UNDEFINED_POLL:
...
break;
case SUPPORT_POLL:
...
break;