I'm trying to get the value of a variable that's in an inner class , which was override by firebase "addValueEventListener" , but when I try to print out the value outside these methods I always get "NULL". This is my code:
DatabaseReference reff = database.getReference().child("Users").child(userId);
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User r= dataSnapshot.getValue(User.class);
username= r.getUserN();
//when I try to print out the value of username here it shows up
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
DatabaseReference ref = database.getReference().child("service").child(b);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Service s = dataSnapshot.getValue(Service.class);
servicename=s.getNomservice();
//same thing as username , i get the value
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
rv = new Rv(username,servicename,dt,tm);
RVId = myRef.push().getKey();
myRef.child(RVId).setValue(rv);
//and here when I print out username and servicename , I get null