简体   繁体   中英

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. 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.

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. 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));
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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