Skip to content

How to access array data from Firebase Firestore?

I have a Firebase Firestore database that looks like this:

image link

And I want to store that data in my ArrayList<>

If You are not understanding anything you can check my previous question Question Link StackOverFlow

Answer

To display the of your favFoods array, please use the following lines of code:

db.collection("fav").document("xQjHVBm0GtUE4VhP3A6m").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                List<String> favFoods = (List<String>) document.get("favFoods");
                for (String favFood : favFoods) {
                    Log.d(TAG, favFood);
                }
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

So as you can see, you should call get() and cast the object to List. The result in the logcat will be:

Chicken
Humburger
vegetables