我正在尝试从解析中检索字符串,然后我想检查该字符串是否为“是”。正确检索字符串,因为我可以看到吐司形式的字符串,但在下一行,我正在检查该字符串是否等于“是”,并根据我在我的解析数据库中设置的数据它应该执行if循环的代码,但它不会。这是代码
ParseQuery<ParseObject> query = ParseQuery.getQuery("RequirementCheck");
query.getInBackground("TVQfYqeX3A", new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
Toast t = Toast.makeText(getActivity(),parseObject.getString("Requirement") , Toast.LENGTH_LONG); //I get this toast of Yes
t.show();
if (parseObject.getString("Requirement") == "Yes") {
Toast t1 = Toast.makeText(getActivity(), "Work", Toast.LENGTH_LONG);
t1.show(); //This if code is not executed
}
else {
Toast t3 = Toast.makeText(getActivity(), "ERROR", Toast.LENGTH_LONG);
t3.show();
}
}
});
答案 0 :(得分:1)
执行以下操作:
ParseQuery<ParseObject> query = ParseQuery.getQuery("RequirementCheck");
query.getInBackground("TVQfYqeX3A", new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
Toast t = Toast.makeText(getActivity(),parseObject.getString("Requirement") , Toast.LENGTH_LONG); //I get this toast of Yes
t.show();
if (parseObject.getString("Requirement").equals("Yes")) {
Toast t1 = Toast.makeText(getActivity(), "Work", Toast.LENGTH_LONG);
t1.show(); //This if code is not executed
}
else {
Toast t3 = Toast.makeText(getActivity(), "ERROR", Toast.LENGTH_LONG);
t3.show();
}
}
});
注意我用.equals()替换了==。要检查字符串,必须执行.equals()而不是Java中的==操作
==检查两个对象是否具有相同的引用。 string.equals()检查两个字符串对象的内容是否相同。在这种情况下,你想要两者中的较晚者。