[英]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.我查看了与此问题类似的 SO 上的各种问答,但还没有找到解决方案。
What I have is an enum which represents different ways to view a TV Guide...我拥有的是一个枚举,它代表了查看电视指南的不同方式......
In the NDroid Application
class在 NDroid Application
类中
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... ...当用户更改视图时,事件处理程序从 0-2 接收一个int
,我想做这样的事情......
In an Android Activity
onClick(DialogInterface dialog, int which)
event handler在 Android Activity
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# 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.我习惯于 C# 枚举和 select/case 语句,它们允许类似上面的内容,我知道 Java 做的事情不同,但我就是不明白我需要做什么。
Am I going to have to resort to if
statements?我将不得不求助于if
语句吗? 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.可能只有 3 个选择,所以我可以这样做,但我想知道如何使用 Java 中的 switch-case 来完成。
EDIT Sorry I didn't completely expand on the issue as I was looking at it as being a generic Java issue.编辑对不起,我没有完全扩展这个问题,因为我认为它是一个通用的 Java 问题。 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
.没有任何特定于 Android 的内容,这就是为什么我没有将其标记为 Android 但枚举是在Application
类中定义的,而我不想切换的代码在Activity
。 The enum is static as I need to access it from multiple Activities.枚举是静态的,因为我需要从多个活动访问它。
The part you're missing is converting from the integer to the type-safe enum.您缺少的部分是从整数转换为类型安全的枚举。 Java will not do it automatically. Java 不会自动执行此操作。 There's a couple of ways you can go about this:有几种方法可以解决这个问题:
guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
即guideView.GUIDE_VIEW_SEVEN_DAY.ordinal()
Determine the enum value represented by the int value and then switch on the enum value.确定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; } }
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.您可能会发现编写自定义valueOf
实现更有用/更不容易出错,该实现将您的整数值作为参数来解析适当的枚举值,并让您集中检查边界。
If whichView
is an object of the GuideView Enum, following works well.如果whichView
是 GuideView Enum 的对象,则以下效果很好。 Please note that there is no qualifier for the constant after case
.请注意, case
之后的常量没有限定符。
switch (whichView) {
case SEVEN_DAY:
...
break;
case NOW_SHOWING:
...
break;
}
枚举不应像您拥有的NDroid.guideView.GUIDE_VIEW_SEVEN_DAY
那样在 case 标签内进行限定,而应删除限定并使用GUIDE_VIEW_SEVEN_DAY
I like a few usages of Java enum:我喜欢 Java 枚举的一些用法:
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;
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.就像@Dhanushka 所说的,省略“switch”中的限定符是关键。
This should work in the way that you describe.这应该按照您描述的方式工作。 What error are you getting?你遇到了什么错误? If you could pastebin your code that would help.如果您可以粘贴您的代码,那将有所帮助。
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
EDIT: Are you sure you want to define a static enum?编辑:您确定要定义静态枚举吗? That doesn't sound right to me.这对我来说听起来不对。 An enum is much like any other object.枚举很像任何其他对象。 If your code compiles and runs but gives incorrect results, this would probably be why.如果您的代码编译并运行但给出了错误的结果,这可能就是原因。
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
I am doing 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 in Switch Statement并在 Switch 语句中使用
switch (mService.getState())
{
case Stopped:
case Paused:
playPause.setBackgroundResource(R.drawable.selplay);
break;
case Preparing:
case Playing:
playPause.setBackgroundResource(R.drawable.selpause);
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.