由于某种原因,无法访问Android If语句
求你了,我需要你的帮助 我在Android上构建这个应用程序时,遇到了这样一个问题:从Firebase数据库检索一个字符串数据并将其分配给一个字符串值,当我尝试使用IF语句来使用内部条件时,我得到的只是编译器检查值条件,而从不输入该语句。 我使用调试模式检查正在运行的应用程序,字符串中存储的值是正确的,if语句中没有问题 我在代码中添加了我遇到问题的部分由于某种原因,无法访问Android If语句,android,if-statement,firebase,firebase-realtime-database,try-catch,Android,If Statement,Firebase,Firebase Realtime Database,Try Catch,求你了,我需要你的帮助 我在Android上构建这个应用程序时,遇到了这样一个问题:从Firebase数据库检索一个字符串数据并将其分配给一个字符串值,当我尝试使用IF语句来使用内部条件时,我得到的只是编译器检查值条件,而从不输入该语句。 我使用调试模式检查正在运行的应用程序,字符串中存储的值是正确的,if语句中没有问题 我在代码中添加了我遇到问题的部分 myRef.addValueEventListener(new ValueEventListener() { public s
myRef.addValueEventListener(new ValueEventListener() {
public static final String TAG = "Testtttttttt";
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
if (value == "START") {
textView.setText(value);
}
}
使用
按值比较字符串是否相等
==通过引用检查相等性,在您的情况下,该值始终为false
阅读了解更多信息。您应该使用:
myRef.addValueEventListener(new ValueEventListener() {
public static final String TAG = "Testtttttttt";
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
// correct compare string1.equals(string2)
if (value.equals("START")) { // You cant compare string1 == string2
textView.setText(value);
}
}
使用value.equalsSTART代替value==start请遵循通常发布在Java Oracle页面上的标准。字符串比较应始终使用stringVar.equals方法
myRef.addValueEventListener(new ValueEventListener() {
public static final String TAG = "Testtttttttt";
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
// correct compare string1.equals(string2)
if (value.equals("START")) { // You cant compare string1 == string2
textView.setText(value);
}
}