"if" not working properly
hey, i'm new to android and i have a problem. here is my code:
Log.v("Test", "" + lv_arr_id[0]); //displays 0
if (lv_arr_id[0] == "0") {
Toast.makeText(longOperationContext, "A", Toast.LENGTH_SHORT).show(开发者_高级运维);
}
else {
Toast.makeText(longOperationContext, "B", Toast.LENGTH_SHORT).show();
}
lv_arr_id[0] has the value "0" and is a string, its external data pulled via json from web. however each time the B toast gets triggered instead of the A toast. the value really is 0, i tested this in the logcat. any ideas why? thanks in advance
== compares the object and not the String contents. Use .equals("0") instead.
It's not that if is not working properly.
In Java, you can't use ==
to compare objects of java.lang.String
class. You need to use equals
method.
Something like:
if (lv_arr[0].equals("0")) {
//
} else {
}
精彩评论