• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to get an arraylist field from firestore?

Hi 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:
Java:
 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 the document
                            for(Object item : task.getResult().getData().values()) {
                                partecipantsArrayList.add(String.valueOf(String.valueOf(item).split(",")));

                                Log.v("vettore", String.valueOf(partecipantsArrayList));
                            }
                        }

                    })

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

                    }
                });

        return partecipantsArrayList;
    }
The problem is that in this way, I get all the field of the document(you can see in the picture below) and even the partecipantsArray is bad formatting, because the name are all in a row, how you can see in the spinner screenshot.
There is a way to get only the array "partecipant" from the document and save it into another array?
 

Attachments

  • error.PNG
    error.PNG
    41.1 KB · Views: 606
  • firestore.PNG
    firestore.PNG
    15.2 KB · Views: 569
So this block of code

Code:
for(Object item : task.getResult().getData().values()) {
      partecipantsArrayList.add(String.valueOf(String.valueOf(item).split(",")));

      Log.v("vettore", String.valueOf(partecipantsArrayList));
}

Is adding the entire string of values, separated by ','. You want to iterate over the list of values

Code:
for(Object item : task.getResult().getData().values()) {
  String[] values = String.valueOf(String.valueOf(item).split(","));
  
  for (String value in values) {
    partecipantsArrayList.add(value);
  }
    
  Log.v("vettore", String.valueOf(partecipantsArrayList));
}

(code is completely off the top of my head, uncompiled, and untested. I'll leave that to you)
 
So this block of code

Code:
for(Object item : task.getResult().getData().values()) {
      partecipantsArrayList.add(String.valueOf(String.valueOf(item).split(",")));

      Log.v("vettore", String.valueOf(partecipantsArrayList));
}

Is adding the entire string of values, separated by ','. You want to iterate over the list of values

Code:
for(Object item : task.getResult().getData().values()) {
  String[] values = String.valueOf(String.valueOf(item).split(","));
 
  for (String value in values) {
    partecipantsArrayList.add(value);
  }
  
  Log.v("vettore", String.valueOf(partecipantsArrayList));
}

(code is completely off the top of my head, uncompiled, and untested. I'll leave that to you)
Thanks for the fast answer...
I just tried your code and..it works! Thank you so much!
Could you help me for one last thing?
I need to remove the last index(I think I can pass to the spinner the partecipantsArraylist minus the last index). Could you tell me how to do that? Then, how you can see in the screenshot, I need to remove the first "[" in the first name and the "]" in the last name, so in the penultimate position. Is it possible?
ps. String[] values = String.valueOf(item.split(",")); is correct
...I was wrong writing two time "String.valueOf"
 

Attachments

  • Screenshot_1543924114.png
    Screenshot_1543924114.png
    78.8 KB · Views: 369
Just solved it!
I modified the code this way:
String[] values = String.valueOf(item).replace("[", "").replace("]", "").split(",");
Now it works!
Thank you so much!
 
Back
Top Bottom