我无法从Android的Firebase数据库中获取用户数据?

时间:2019-03-20 02:31:23

标签: android firebase firebase-realtime-database

我的数据库结构是:rootNode-> node1-> Uid-> RandomId(我从push.getKey()方法获得)然后是我的数据->(名称,电子邮件,电话)

projectName:
     SampleUsers
           Uid
              RandomKey1(i get this random key by push.getKey() method)
                         Name : User1
                         Email : user@email.com
                         Phone : +1234567890
              RandomKey2
                         Name : User1
                         Email : user@email.com
                         Phone : +1234567890 

我想在用户登录时在其个人资料页面中向用户显示详细信息。但是我无法获得那个随机密钥;

mUserRef =  firebaseDatabase.getReference("Users").child(mAuth.getCurrentUser().getUid());

1 个答案:

答案 0 :(得分:0)

要显示给定快照的所有子节点,请遍历DataSnapshot.getChildren()。例如:

DatabaseReference userRef =  firebaseDatabase.getReference("Users").child(mAuth.getCurrentUser().getUid());
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            System.out.println(childSnapshot.getKey()); // the push ID
            System.out.println(childSnapshot.child("Name").getValue(String.class));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}