This is an example of my code. Right now, if I print the userName variable inside onDataChange, it works fine. But if I try to print the username outside, after the listener, it will print "null". How can I store a variable with the data I want inside onDataChange ()?
public class FireBaseTest extends .... {
String userName;
...
mDatabase.child("Users").child(key).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName = dataSnapshot.child("userName").getValue(String.class);
System.out.println(userName);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
...
System.out.println(userName);
EDIT / UPDATE: Therefore, before I had a process that would take on the value that they were listening to and do something with it. I just made some changes to this process and added it to the onDataChanged method to end my suffering. Although this is not entirely satisfactory, it worked. Thanks to all who responded.