简体   繁体   中英

How to get all child values from firebase realtime database and store it in an array list

I am familiar with firestore but uncertain how to get the values from realtime database. here is my database below data base image

As u can see I have UID under which I have stored multiple document references. here how I did:

           firebaseDatabase.getReference(firebaseAuth.getCurrentUser().getUid()).push().setValue(documentReference.getPath());

I want to get the whole document references under the UID and store it in an array list or just iterate through each document reference. Thank you

Try below.

DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference(firebaseAuth.getCurrentUser().getUid());

ArrayList<String> docRefList =new ArrayList<>();    

mDatabaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
              String docRef = postSnapshot.getValue(String.class);
              docRefList.add(docRef);
        }
   }
   @Override
   public void onCancelled(DatabaseError error) {
        Log.d(TAG, "Error Load");
   }
});

Remember that onDataChange() is an async call to read from Firebase DB. Therefore, for any post processing of data you receive, you have to wait until the data read is completed.

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