I am currently doing some school android project and I need help.I have variable that takes some value in database listener,and I have to use that value outside listener.Can you please help me to find way to correct initialization and using that variable.Thank you My code is below
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("user");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Get map of users in datasnapshot
names= getAllNames((Map<String,Object>) dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});
String p= names.get(1);
this method getAllNames is working fine,I tested it .P in this case is null because it is usingf "names" outside listener and variable "names" is problem,but in listener it shows correct value.