0

I have two documents in one collection, each document has an object in it, so me i want to read them differently.

data.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>(){   
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        if (!documentSnapshot.exists()){

        }
    }
});
Prince
  • 1
  • 2

1 Answers1

0

Below code read each document iteratively

db.collection("anyCollection")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            QuerySnapshot querySnapshot = task.getResult();
                            List<DocumentSnapshot> documentSnapshots = querySnapshot.getDocuments();// add check for each type of document...

                            for(DocumentSnapshot snapshot: documentSnapshots){
                                HashMap<String,Object> map = new HashMap<>();
                                map = (HashMap<String, Object>) snapshot.getData();
                                ///https://stackoverflow.com/a/1066607/3496570
                                for (Map.Entry<String, Object> entry : map.entrySet()) {
                                    String key = entry.getKey();
                                    Object value = entry.getValue();
                                    // ...
                                }
                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                });
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300