-1

please I need your help i am building this application on Android, I ran into this problem where one String data is being retrieved from Firebase database and assigned to a String value, when I try to use (IF) statement to use inside condition, all I get is compiler checking the value condition and never enter the statement. I check the running application using Debugging mode, the value stored into the string is correct and if Statement has no problem in it.

I added part of the code where I have the problem

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);


            }
        }
RamiRihawi
  • 11
  • 5
  • use value.equals("START") instead value == "START" – Abhi Dec 26 '16 at 18:25
  • please follow standards that are usually posted on the Java Oracle page amongst others. String comparisons should always use stringVar.equals() method – Manny265 Dec 26 '16 at 19:36

2 Answers2

2

Use

value.equals("START")

to compare Strings for equality by value.

== checks for equality by reference, which will always be false in your case.

Read http://www.javatpoint.com/string-comparison-in-java for more information.

Maurice Döpke
  • 367
  • 3
  • 9
1

You should use:

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);


        }
    }
HerberthObregon
  • 1,811
  • 19
  • 23