1

Hy guys, I need to retrieve an arraylist saved in firestore which contains a list of partecipants that I have to put in a spinner. I can't figure out how to get the entire arraylist. That's my code:

public ArrayList<String> getPartecipantsList(){
    String email = getEmail();
    String groupTitle = getTitleBar();
    DocumentReference docRef = db.collection("users").document(email).collection("Group").document(groupTitle);

    docRef.get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {


                        DocumentSnapshot document = task.getResult();
                        //Extracting participants ArrayList from each document
                        for(Object item : task.getResult().getData().values()) {
                            partecipantsArrayList.add(item.toString());
                            Log.v("vettore", String.valueOf(partecipantsArrayList));
                        }
                    }

                })

            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });

    return partecipantsArrayList;
}

Then for the spinner:

public void load_spinner(){
    partecipantsArrayList = getPartecipantsList();
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, partecipantsArrayList);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

That's a picture of the database enter image description here

The problem is that in this way, I get all the field in the document..how can i get only the partecipant field?enter image description here

Nicola
  • 301
  • 3
  • 20
  • Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Dec 04 '18 at 09:55
  • This Is not a duplicate..I need to get an Array from firestore, but the document has other field that I don't want to put in my spinner. I can't use getString because It isn't a string buy and Array. In your other answer I can't find the solution – Nicola Dec 04 '18 at 10:02
  • Is the exact same duplicate. Please also check the second duplicate, where it is about an `ArrayList`. – Alex Mamo Dec 04 '18 at 10:16
  • Man, did you read my question entirely? Did you see the screenshot? Is a different question. I need a field of a document which Is an Array, not a list of a document in a collection. I know the collection name and the document name, and from that document I want to get the field "partecipant", but I can't use getString, because that field Is not a string type, but and arraylist – Nicola Dec 04 '18 at 10:29
  • It is the exact same thing. You can never return `partecipantsArrayList` as a result of a method. – Alex Mamo Dec 04 '18 at 10:37
  • Sorry but I don't understand...this is a similar question https://stackoverflow.com/questions/49016643/convert-firebase-field-array-to-list the difference is that I also have another field "numPartecipant" in my document, that I don't want to retrieve..If I can't do that, how can I divide all the name I get(see my screenshot) to put them in single line in the spinner? – Nicola Dec 04 '18 at 10:45

0 Answers0