2

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

10
  • So you want to get the content of the favFoods array right? Then please show a more detailed database schema and what have you tried so far. Commented Aug 25, 2021 at 8:13
  • Yes I want favFoods array Commented Aug 25, 2021 at 8:14
  • @AlexMamo did you check my previous question because I am asking related to that question link Commented Aug 25, 2021 at 8:16
  • Is this db.collection("data").document("one") your structure? Commented Aug 25, 2021 at 8:17
  • Yes!. it looks like this db.collection("fav").document("randomId") Commented Aug 25, 2021 at 8:21

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.