7

I am using Cloud Firestore and there is a array data in field. So how can I get data from array individually?

enter image description here

1
  • Do you want entire Array? Commented Sep 27, 2018 at 13:19

1 Answer 1

9

To get the values within ip_range array, please use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collection("aic").document("ceo").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Object> map = document.getData();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    if (entry.getKey().equals("ip_range")) {
                        Log.d("TAG", entry.getValue().toString());
                    }
                }
            }
        }
    }
});

Another approach would be:

rootRef.collection("products").document("06cRbnkO1yqyzOyKP570").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                ArrayList<String> list = (ArrayList<String>) document.get("ip_range");
                Log.d(TAG, list.toString());
            }
        }
    }
});

In both cases, the output in your logcat will be:

[172.16.11.1, 172.16.11.2]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanx @Alex Mamo. It is working. But how did you know the collection & document name, however I have wrapped it in the image.
Good to hear that it worked! I knew the collection and document name because of this :)
second solution above, i got warning "Unchecked cast: 'java.lang.Object' to 'java.util.ArrayList<java.lang.String>'"
@LatiefAnwar You can use an annotation to suppress it.

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.