如何检查枚举 class 中的值?
How to check values inside enum class?
我想检查从 json 对象返回的标签是否具有枚举 class 中的值之一。
Gson gson = new Gson();
AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
for(Enum p : Enum.values()){
if(p.name().equals(result.tags)){
Intent in1 = new Intent(Analyze.this, Analyze2.class);
startActivity(in1);
}else {
for (final Caption caption : result.description.captions) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
speakOut("Result:" + caption.text);
}
}, 100);
}
}
}
这是我的枚举class
public enum Enum {
person,
people,
people_baby,
people_crowd,
people_group,
people_hand,
people_many,
people_portrait,
people_show,
people_tattoo,
people_young
}
这是返回的标签...
"tags":["teddy","indoor","clothing","necktie","person","bear","wearing","green","brown","stuffed","sitting","man","bow","neck","face","shirt","blue","hat","close","grey","laying","black","glasses","head","bed","white","holding","cat","sleeping"]}
我的问题是它总是转到 else 语句。您认为代码有什么问题?
我不确定我是否完全理解你想在这里实现的目标,而且我不知道这个 'Tag' 对象是什么,但我猜你想做这样的事情:
Gson gson = new Gson();
AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
boolean hasTag = false;
for (Tag t : result.tags) {
if (Enum.hasTag(t.getTagName())) {
hasTag = true;
break;
}
}
if (hasTag) {
Intent in1 = new Intent(Analyze.this, Analyze2.class);
startActivity(in1);
} else {
for (final Caption caption : result.description.captions) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
speakOut("Result:" + caption.text);
}
}, 100);
}
}
你的枚举是这样的:
public enum Enum {
person,
people,
people_baby,
people_crowd,
people_group,
people_hand,
people_many,
people_portrait,
people_show,
people_tattoo,
people_young;
public static boolean hasTag(String tag) {
for (Enum e : values()) {
if (e.name().equals(tag))
return true;
}
return false;
}
}
我想检查从 json 对象返回的标签是否具有枚举 class 中的值之一。
Gson gson = new Gson();
AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
for(Enum p : Enum.values()){
if(p.name().equals(result.tags)){
Intent in1 = new Intent(Analyze.this, Analyze2.class);
startActivity(in1);
}else {
for (final Caption caption : result.description.captions) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
speakOut("Result:" + caption.text);
}
}, 100);
}
}
}
这是我的枚举class
public enum Enum {
person,
people,
people_baby,
people_crowd,
people_group,
people_hand,
people_many,
people_portrait,
people_show,
people_tattoo,
people_young
}
这是返回的标签...
"tags":["teddy","indoor","clothing","necktie","person","bear","wearing","green","brown","stuffed","sitting","man","bow","neck","face","shirt","blue","hat","close","grey","laying","black","glasses","head","bed","white","holding","cat","sleeping"]}
我的问题是它总是转到 else 语句。您认为代码有什么问题?
我不确定我是否完全理解你想在这里实现的目标,而且我不知道这个 'Tag' 对象是什么,但我猜你想做这样的事情:
Gson gson = new Gson();
AnalysisResult result = gson.fromJson(data, AnalysisResult.class);
boolean hasTag = false;
for (Tag t : result.tags) {
if (Enum.hasTag(t.getTagName())) {
hasTag = true;
break;
}
}
if (hasTag) {
Intent in1 = new Intent(Analyze.this, Analyze2.class);
startActivity(in1);
} else {
for (final Caption caption : result.description.captions) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
speakOut("Result:" + caption.text);
}
}, 100);
}
}
你的枚举是这样的:
public enum Enum {
person,
people,
people_baby,
people_crowd,
people_group,
people_hand,
people_many,
people_portrait,
people_show,
people_tattoo,
people_young;
public static boolean hasTag(String tag) {
for (Enum e : values()) {
if (e.name().equals(tag))
return true;
}
return false;
}
}