2

I'm trying to convert a field which has an array in Firebase Cloud Firestore. The field is stored in a document and contains 10 values in the field.

I can get the data using the following code however I would like to know if its possible to convert this data into a List?

public void getAllowedPostcodes(){
    DocumentReference docRef = 
db.collection("AllowedPostcodes").document("tmGzpfFPFTwGw28uS8y1");

    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document != null) {
                    Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());

                } else {
                    Log.d(TAG, "No such document");
                }
            } else {
                Log.d(TAG, "get failed with ", task.getException());
            }
        }
    });
}

I have tried the following however, it doesn't compile saying:

"Cannot resolve constructor 'ArrayList(java.util.Map<java.lang.String,java.lang.Object>)"

This is my code:

List<String> allowedData = new ArrayList<String>(task.getResult().getData());

The database structure is as follows: enter image description here

2
  • try using new ArrayList<String>((List)task.getResult().getData().values()); Commented Feb 27, 2018 at 19:58
  • Thanks for the suggestion @RosárioPereiraFernandes. Unfortunately it doesn't work. It throws the following error: java.lang.ClassCastException: java.util.HashMap$Values cannot be cast to java.util.List Commented Feb 27, 2018 at 20:26

1 Answer 1

2

Create a forEach loop to iterate through each field, cast it to String and add it to the ArrayList:

docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null) {
                        Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
                        for(Object item : task.getResult().getData().values())
                            allowedData.add(item.toString());
                    } else {
                        Log.d(TAG, "No such document");
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That still gives me an exception: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
I managed to get it to work using for(Object item : task.getResult().getData().values()) { allowedData.add(item.toString());
Glad you did. I was gonna suggest that next :) .. I've updated my answer to match 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.