Getting an ArrayList from Firestore and the document name

Nicola :

I'm new on Google Firebase and I'm trying to learn something about it. I'm doing an Android app where you can create a group of person and set the title of the group..then, in the "group page", you can see all your group in a listview. The structure of my firestore db is something like this:

users --> email(document) ---> Group(collection) --> GroupName(Document) and the group name document contains the partecipants arrayList (partecipant 0 : Name1, partecipant1: name2 etc).

I would like to retrieve the document id(which is the group title) and the arrayList of partecipants, but I don't know of to use the for each in the code...

This is my code:

public void load_list_view(){

    String email = getEmail();
    final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();

    docRef.get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {


                        titleArray.add(documentSnapshot.getId());
                        titleString = documentSnapshot.getId();

                        partecipantsArray.add(documentSnapshot.getString("partecipant"));
                        num_partecipants =  partecipantsArray.size();
                        numArray.add(num_partecipants);
                        trash = R.drawable.trash_icon;
                        firstChar = Character.toString(titleString.charAt(0));
                        firstCharArray.add(firstChar);
                        customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
                        listView.setAdapter(customAdapter);


                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(GroupActivity.this, e.getStackTrace().toString(), Toast.LENGTH_LONG).show();

                }
            });

}

with titleArray.add(documentSnapshot.getId()); it retrieve a random ID and I can't understand why.

I haven't found enough documentation on Internet about Arraylist and firestore.

Dipayan Ray :

First of all, to get all the documents in a collection you should write your code differently as shown in this documentation.

db.collection("cities")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Log.d(TAG, document.getId() + " => " + document.getData());
                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

Secondly, if you are retrieving an ArrayList you should use (ArrayList<String>) documentSnapshot.get("key") instead of documentSnapshot.getString("key").


Thirdly, you are getting random Id because with this line of code (mentioned below) firebase is generating a new document reference with a random id. Reference Link.

    final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();

For your help, I have tweaked your code and you can try this code and check if it's working or not.

public void load_list_view() {

        String email = getEmail();
        final DocumentReference docRef = firestore.collection("users").document(email);

        docRef.collection("Group")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        for (QueryDocumentSnapshot document : queryDocumentSnapshots) {

                            //Extracting Group name from each document

                            titleString = document.getId();
                            titleArray.add(titleString);

                            //Extracting participants ArrayList from each document

                            partecipantsArray.add((ArrayList<String>) document.get("participant"));
                            numArray.add(num_partecipants);
                            firstChar = Character.toString(titleString.charAt(0));
                            firstCharArray.add(firstChar);

                        }

                        num_partecipants = partecipantsArray.size();
                        numArray.add(num_partecipants);
                        trash = R.drawable.trash_icon;
                        firstChar = Character.toString(titleString.charAt(0));
                        firstCharArray.add(firstChar);
                        customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
                        listView.setAdapter(customAdapter);

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        //HANDLE EXCEPTION
                    }
                });
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=107196&siteId=1