我定义了这个枚举:
public enum UsageType {
START("start"),
PAUSE("pause"),
RESUME("resume"),
STOP("stop"),
DESTROY("destroy");
private final String mType;
private UsageType(String type) {
mType = type;
}
/**
* Get the string representation of the UsageType
* @return string representation of the UsageType
*/
public String getAsText() {
return mType;
}
}
在另一个类中,我有一个带字符串的约束器,我想用该字符串创建一个枚举:
public class AppUsage {
private String mActivityName;
private String mFormattedTime;
private UsageType mUsageType;
public AppUsage(String activityName, String formattedTime, String usageType) {
mActivityName = activityName;
mFormattedTime = formattedTime;
mUsageType = mUsageType.valueOf(usageType); //HERE STRING TO ENUM!
}
//Setters and Getters....
这是我得到的错误:
java.lang.IllegalArgumentException: start is not a constant in com.embedonix.mobilehealth.serverwork.usage.UsageType
at java.lang.Enum.valueOf(Enum.java:198)
at com.embedonix.mobilehealth.serverwork.usage.UsageType.valueOf(UsageType.java:6)
答案 0 :(得分:3)
试试UsageType.valueOf(usageType.toUpperCase())
。当您使用该方法时,该字符串应与常量名称(START
)匹配,并且案例很重要。
请注意,我使用的是枚举名UsageType
,因为valueOf
是一种静态方法。所以,你应该需要一个实例。
另请注意,valueOf
方法抛出IllegalArgumentException
运行时激活,如果没有名称的常量存在。
答案 1 :(得分:3)
如果使用常量名称类型(大写的那个),而不是您在构造函数上通过参数传递的内部名称。
String str = "START"; // as example...could be "PAUSE", or "DESTROY", etc.
UsageType type = UsageType.valueOf(str);
删除此构造函数参数..没用。简单地说就是这样:
public enum UsageType {
START,
PAUSE,
RESUME,
STOP,
DESTROY;
}
对于 getAsText(),只需使用内置的 UsageType.name()。
答案 2 :(得分:1)
这个怎么样?
public AppUsage(String activityName, String formattedTime, String usageType) {
mActivityName = activityName;
mFormattedTime = formattedTime;
for(UsageType type : UsageType.values())
{
if(type.getAsText().equals(usageType))
mUsageType = type;
}
}
答案 3 :(得分:1)
当我想将字符串转换为枚举时,我总是使用查找映射 将此添加到您的枚举
private static final Map<String, UsageType> lookup = new HashMap<String, UsageType>();
static {
for (final UsageType s : EnumSet.allOf(UsageType.class)) {
lookup.put(s.getAsText(), s);
}
}
static public UsageType fromString(final String name) {
return lookup.get(name);
}
现在要将您的字符串转换为枚举,您需要做的就是UsageType.fromString("someString");
答案 4 :(得分:0)
我所观察到的是基于枚举我们可以获得价值但不是通过传递价值来获得枚举名称。 即试试这个它对我来说很好。希望它对你有所帮助。
public enum UsageType {
START("start"),
PAUSE("pause"),
RESUME("resume"),
STOP("stop"),
DESTROY("destroy");
private final String mType;
UsageType(String type) {
mType = type;
}
public String getAsText() {
return mType;
}
}
public class AppUsage {
private String mActivityName;
private String mFormattedTime;
public static UsageType mUsageType;
public AppUsage(String activityName, String formattedTime, String usageType) {
mActivityName = activityName;
mFormattedTime = formattedTime;
mUsageType = UsageType.valueOf(usageType); //HERE STRING TO ENUM!
}
public String getmActivityName() {
return mActivityName;
}
public void setmActivityName(String mActivityName) {
this.mActivityName = mActivityName;
}
public String getmFormattedTime() {
return mFormattedTime;
}
public void setmFormattedTime(String mFormattedTime) {
this.mFormattedTime = mFormattedTime;
}
public UsageType getmUsageType() {
return mUsageType;
}
public void setmUsageType(UsageType mUsageType) {
AppUsage.mUsageType = mUsageType;
}
public static void main(String args[]){
// AppUsage au=new AppUsage("a", "a","START");
UsageType v[]=mUsageType.values();
UsageType u=mUsageType.valueOf("START");
System.out.println(u.getAsText());
}
}
希望它可以解决您的问题