简体   繁体   中英

“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 {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM