-1

I want to open a specific activity based on the data I retrieve from the firebase realtime database...

    Firebase.setAndroidContext(this);
    mRef=new Firebase("https://test-new-f2637.firebaseio.com/Name");
    mValueView=(TextView) findViewById(R.id.valueView);
    btn=(Button) findViewById(R.id.button);
    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            value = dataSnapshot.getValue(String.class);
            mValueView.setText(value);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String n=mValueField.getText().toString();
            if(n=="yes")
            {
                func1();
            }
            else
            {
                func2();
            }
        }
    });
}
 public void func1()
 {
     Intent intent = new Intent(this, first.class);
     startActivity(intent);
 }

public void func2()
{
    Intent intent = new Intent(this, second.class);
    startActivity(intent);
    }
}

If I retrieve "yes" from the firebase databse then the first activity should be opened or else the second activity should open.

But the problem is that by retrieving the value as "yes" then also the second activity is opening

PS:-I'am new to android and firebase

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53

3 Answers3

1

n=="yes" will always be false

Have a read of this https://www.codejava.net/coding/10-common-mistakes-every-beginner-java-programmer-makes

https://www.codejava.net/coding/10-common-mistakes-every-beginner-java-programmer-makes#Comparison

The == operator compares two objects physically (their memory addresses) whereas the equals() method compares two objects semantically (their information). And in most of the cases, we should compare two objects meaningfully using the equals() method.

Try:

        if("yes".equals(n)) {
            func1();
        } else {
            func2();
        }
Blundell
  • 75,855
  • 30
  • 208
  • 233
0

Firstly, what is the value returned by Firebase, that you display on a Textview ?

Secondly, prefer using the method String#equals() to compare Strings.

Instead of if(n=="yes") , use if(n.equals("yes"))

pocus
  • 51
  • 7
0

you are getting text from another thextview and when you compare String value then don't use "==" use this ".equals("text")" for comparision in string :-

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String n=mValueView.getText().toString();
            if(n.equals("yes"))
            {
                func1();
            }
            else
            {
                func2();
            }
        }
    });
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17