简体   繁体   English

Android,Firebase-尝试检索多个子节点时,数据库仅将单个子节点作为字符串返回

[英]Android, Firebase - database only returns single child node as string when trying to retrieve multiple child nodes

I've just started trying to use Firebase in my Android application. 我刚刚开始尝试在Android应用程序中使用Firebase。 I can write data into the database fine but I'm running into a problem when trying to retrieve data. 我可以将数据写到数据库中,但是尝试检索数据时遇到了问题。

My database is structured as below 我的数据库结构如下
在此处输入图片说明


My method for retrieving the data: 我检索数据的方法:

public void getCurrentUserData() {

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        FirebaseUser loggedUser = firebaseAuth.getCurrentUser();
        String uid = loggedUser.getUid();

        DatabaseReference userRef = database.getReference().child("Users").child(uid);

        userRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //crashes because user is not a string
                //User user = dataSnapshot.getValue(User.class);

                //works because function is returning first child of uID as a string
                String user = dataSnapshot.getValue().toString();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }


When debugging: 调试时:

dataSnapshot = "DataSnapshot { key = bio, value = test }"


I was expecting this to return all children contained within uid (bio, dob, firstName, joinDate, lastName, location) and put them into my User object but it actually looks as if its only returning the first child node (bio) as a string. 我期望这将返回uid中包含的所有子代(bio,dob,firstName,joinDate,lastName,位置)并将其放入我的User对象,但实际上看起来好像它仅以字符串形式返回了第一个子节点(bio) 。

Why is this happening and how do I retrieve the full set of child nodes? 为什么会发生这种情况,以及如何检索完整的子节点集?

Any help appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

If you want to get all properties of the user, you will either need to create a custom Java class to represent that user, or you will need to read the properties from the individual child snapshots in your own code. 如果要获取用户的所有属性,则需要创建一个自定义Java类来表示该用户,或者需要在自己的代码中从各个子快照中读取属性。 For example: 例如:

userRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        String firstName = dataSnapshot.child("firstName").getValue(String.class);
        String lastName = dataSnapshot.child("lastName").getValue(String.class);
        ...
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

Alternatively you can loop over the properties with: 或者,您可以使用以下方法遍历属性:

userRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot propertySnapshot: dataSnapshot.getChildren()) {
            System.out.println(propertySnapshot.getKey()+": "+propertySnapshot.getValue(String.class));
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM