简体   繁体   中英

How can I iterate through children of multiple push keys in Firebase Realtime Database?

I are trying to retrive lecture information. The information is structured like this: 在此处输入图像描述

I am trying to store details of each lecture in an array. 在此处输入图像描述

To iterate through the children try this:

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Lectures").child("Saturday,March 03");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
   String batchname=datas.child("batch_name").getValue().toString();
          //etc
     }
  }
    @Override
   public void onCancelled(DatabaseError databaseError) {
  }
  });

the for(DataSnapshot datas: dataSnapshot.getChildren()) will let you iterate inside the push keys.

dataSnapshot.getChildren() will give you the direct children, which are the push keys in this case. Then using the for loop you will be able to access the data inside these keys

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