0

I'm having some trouble creating an arrayList of names taken from a firebase database.I risk taking the data individually, but I can't put it in an arrayList, which is always empty. `

    db = FirebaseFirestore.getInstance();
    ArrayList<String> names = new ArrayList<String>();
    db.collection("Persone").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()){
                for (QueryDocumentSnapshot document : task.getResult()){
                    String nome = document.getString("Nome");
                    names.add(nome);
                }
            }
        }
    });`

This is my database

Matteo118
  • 1
  • 1
  • How are you confirming the names aren’t added? Could be this issue: https://stackoverflow.com/q/57330766/506796 – Tenfour04 Jan 31 '22 at 16:50
  • There is no way you can simply use the `names" list outside the callback. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Feb 01 '22 at 08:20

0 Answers0