请我需要您的帮助我正在在Android上构建此应用程序,我遇到了这个问题,其中一个字符串数据从Firebase数据库中检索并分配给字符串值,当我尝试使用(如果)语句以使用内部条件时,我得到的就是编译器检查价值条件,切勿输入声明。我使用调试模式检查运行应用程序,存储在字符串中的值是正确的,如果语句中没有问题。
我添加了我有问题的代码的一部分
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);
}
}
使用
value.equals("START")
比较按值进行平等的字符串。
==通过参考检查平等,在您的情况下始终是错误的。
阅读http://www.javatpoint.com/string-comparison-in-java有关更多信息。
您应该使用:
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);
}
}