Reputation: 35
It's showing something, but from the wrong user, not the signed-in user. In fact, it is the latest registred user. Any suggestions? Here is my code:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getInstance().getReference("userInfos");
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
vorname.setText(userInformation.vorname);
nachname.setText(userInformation.nachname);
alter.setText(userInformation.alter);
sprachen.setText(userInformation.sprachen);
System.out.println("Prev: " + s);
}
Upvotes: 2
Views: 147
Reputation: 12005
Well, the type of listener you're using is the ChildAdded one that will ge triggered when you add a new user (that's why you're getting the last added user) To get the current user data user, use a value event listener and pass the logged user id:
String UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = database.getInstance().getReference("userInfos");
ref.child(UID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
vorname.setText(userInformation.vorname);
nachname.setText(userInformation.nachname);
alter.setText(userInformation.alter);
sprachen.setText(userInformation.sprachen);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 1
Reputation: 138824
When you are using the following line of code:
System.out.println("Prev: " + s);
The s
variable holds a value of type String that is actually coming from the second argument of the overridden method onChildAdded()
. According to the offical documentation, the second argument can be also written as:
onChildAdded(DataSnapshot snapshot, String previousChildName)
That's why you are printing a wrong name. So the second argument is actually the name of the previous child.
Upvotes: 1