I wanted to check if the tags returned from a json object has one of the values inside an enum 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);
}
}
}
Here is my Enum class
public enum Enum {
person,
people,
people_baby,
people_crowd,
people_group,
people_hand,
people_many,
people_portrait,
people_show,
people_tattoo,
people_young
}
This are the returned tags...
"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"]}
My problem is that it always goes to the else statement. What do you think is wrong with the code?
I'm not sure I fully understand what you want to achieve here, And I don't know what is this 'Tag' object, but I guess you want to do something like this:
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);
}
}
And your Enum like this:
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;
}
}