1

When i first click the button, the value of pass variable is null When i click it for the second time and change the value of edit1, the pass value is the value of the edit1 from the first click Why is that? This is my code

database = FirebaseDatabase.getInstance();
    myRef = database.getReference("user").child(edit1.getText().toString()).child("password");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            pass = dataSnapshot.getValue().toString();
        }

        @Override
        public void onCancelled(DatabaseError error) {
        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Your code doesn't show how you use `pass`, but most likely that is outside of `onDataChange`. You're seeing the effects of the fact that `onDataChange` is called asynchronously, which means the code *inside* `onDataChange` is invoked *after* the code *after* it. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Dec 24 '18 at 19:29

0 Answers0