1

I have a static method that gets all the documents in a collection. I am having trouble creating an array of these documents to return. How could I do that? Let us call the Array list 'allItems'. My approach was to add each item that is created inside the for loop to an the allItems array list, but this list is null when I return it and android studio is prompting me to make the array list final.

public static ArrayList<Item> geAllFromFireStore() {

        FirebaseFirestore db = FirebaseFirestore.getInstance();
        ArrayList<Item> allItems = new ArrayList<>();

        db.collection("Items")
                .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());

                                String name = document.get("name") + "";
                                String categoryName = document.get("category") + "";

                                Item item = new Item(name, category);

                                // I would like to add these items to an array list and return that array list

                            }
                        } else {
                            Log.w(TAG, "Error getting documents.", task.getException());
                        }
                    }
                });

        return allItems;
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Oamar Kanji
  • 1,824
  • 6
  • 24
  • 39

2 Answers2

1

I write it in Kotlin, so please adapt this in your code.

// Declare variable that use to collect DocumentSnapshot
private lateinit var list: MutableList<DocumentSnapshot>

// In addOnCompleteListener…
if(task.isSuccessful){
    lstMAP = mutableListOf<DocumentSnapshot>()
    lstMAP.clear()
    lstMAP.addAll(task.result!!.documents)
} else {
    Log.d("doc", "0 doc")
}
Hello World
  • 740
  • 5
  • 18
1

but this list is null when I return it and android studio is prompting me to make the array list final.

There's nothing wrong with making it final, and given that code, it's impossible for it to return null, but very possibly might be empty, because it's actually returning before Firebase even attempts to make a remote database query.

That's the problem with being asynchronous - you need to wait for the result before you can return it.

For example, parameterize the listener that gets the snapshot

public static void getAllFromFireStore(OnCompleteListener<QuerySnapshot> listener) {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    db.collection("Items").get().addOnCompleteListener(listener);
} 

And that's it.

From there, refactor the rest of the code to pass in the listener you've already written rather than actually expect an object to be returned from the method call

API.getAllFromFireStore(new OnCompleteListener<QuerySnapshot>() {
    @Override 
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            // TODO: get data 
        } 
});

From inside of that, you can populate a RecyclerView Adapter, for example

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245