简体   繁体   中英

How to get all the values from the list in firebase database using java?

I want to get all the field values in a list. I tried but I got items as null. Please help me with this. I tried to get all the values using addOnChildEventListener(), but got all the elements as null. So I have no solution that how to get all those values and add them to the appropriate lists as typed in the code.

在此处输入图片说明

Here is the code :

    private void listViewDataListener() {
    FirebaseDatabase
            .getInstance()
            .getReference(classCreatorPhoneNo + "/" + "class_list" + "/" + classUuid + "/" + "attendance" + "/" + attendanceDate + "/" + subjectUuid + "/" + lectureRank)
            .addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
                    studentStatusList.add(snapshot.child("student_status_list").getValue(String.class));
                    studentUuidList.add(snapshot.child("student_uuid_list").getValue(String.class));
                    cardBackgroundColor.add(snapshot.child("background_color_list").getValue(String.class));
                }

                @Override
                public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot snapshot) {

                }

                @Override
                public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                }

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

                }
            });
}

The problem is in all three of your getValue calls, so let's look at one:

studentStatusList.add(snapshot.child("student_status_list").getValue(String.class));

If we look at the student_status_list in your screenshot, it is not a single String value, but rather a list of values. So calling getValue(String.class) on the student_status_list snapshot will return null , because the value of that node is not a string.

If you want to show all values under student_status_list , you can loop over the snapshots children to do so:

for (DataSnapshot statusSnapshot: snapshot.child("student_status_list").getChildren()) {
  String status = statusSnapshot.getValue(String.class));
  studentStatusList.add(status);
}

You'll have to do the same for the other nodes: any time you have multiple children of which you don't know the keys, you can loop over the getChildren() to access those child nodes.

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