0

I am trying to get data in a two specific field in each document where one field is a integer and the other is a string. I want each field to be stored in an array separately.

 db.collection("Activity")
                .whereEqualTo("plan_id", plan_id)
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            List<Long> progressList = new ArrayList<>();
                            List<String> titleList = new ArrayList<>();
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.get("progress"));

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

I've tried

int[] prog = (int[]) document.get("progress");
String[] title = (String[]) document.get("title");

but no luck...

1
  • Do you want to store those data values into Array or List? Because I saw there were 2 declarations of List for progress and title, respectively. Commented Oct 28, 2019 at 0:53

1 Answer 1

1

I am not familiar with Firestore, but it seems that the QueryDocumentSnapshot is similar to Map<String, Object>. Following code snippet shows how to store those values into List as declared in your code - progressList and titleList, respectively.

List<int> progressList = new ArrayList<>();
List<String> titleList = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
    progressList.add(Integer.valueOf(document.get("progress").toString()));
    titleList.add(document.get("title").toString());
}

And if you still want to use Array to store values, you can use API ArrayList.toArray() as follows:

int[] prog = new int[progressList.size()];
prog = progressList.toArray(prog);

String[] title = new String[titleList.size()];
title = titleList.toArray(title);
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.