0

I'm using Firebase in android studio, and I want to make a signup and login project. I'm able to save data to the Firebase, but I can't retrieve data.

In every example, it's realtime --like, we set up a listener first and save data-- but mine is just storing and later retrieving.

Is there a way to just retrieve data? (I wish you could understand :( )

public void onLogIn(View v){
    reference2 = fbData.getReference(loginId.getText().toString());
    reference2.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String gotPw = dataSnapshot.getValue(String.class);
            if(gotPw == loginPw.getText().toString()){
                Log.d("TEST","DONE");
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d("TEST","ERROR");
        }
    });

}

This code doesn't even call the onDataChange method.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Huzi
  • 3
  • 1
  • 2
    Retrieving data from the Firebase Database is done by attaching a listener. If neither your `onDataChange()` nor your `onCancelled()` is being called, it's most likely that your not connected to the network. – Frank van Puffelen Jan 14 '17 at 00:30
  • 1
    I don't know if you've already checked in other ways if onDataChange is called, but looking at the code above, I wouldn't rely on hitting the "DONE" Log. You're comparing Strings, you should use the .equals() method to compare Strings, not ==. http://stackoverflow.com/a/767379/5251523 – Lewis McGeary Jan 14 '17 at 01:31

1 Answers1

1

In java == operator to compare string doesn't always return true even if the string values might be the same.

Try doing:

if(gotPw.equals(loginPw.getText().toString())){
            Log.d("TEST","DONE");
        }
Aman Arora
  • 109
  • 1
  • 11