I have a custom class (class has a constructor and setters/getters)
class Test() {
String field1;
String field2;
String field3;
}
and this is my Firestore snapshot
Fields "0" and "1" are my iterators, depending on how many elements have been created.
I’m using this line of code to determine how many times to read
for (int i = 0; i < documentSnapshot.getData().size(); i++)
I need to get all elements, make a new object Test for each of them (o1{field1..3} .. oN{field1..3})
and store all objects in ArrayList<Test>
but I can’t figure out how.
2
Answers
If you need to get each field (0, 1, etc) from the ABCD document and create a
List<Test>
, first of all, please define your Test class like this:And right after that, please use the following lines of code:
According to your screenshot, the result in the logcat will be:
On the other hand, if you want to map an array of custom objects into a list of custom objects, then please see my answer from the following post:
To read a map of nested custom objects from Firestore in Android, you can follow these steps:
Define a POJO (Plain Old Java Object) class to represent your custom object.
Create a reference to the Firestore collection or document containing the map of nested custom objects.
Call the get() method on the reference to retrieve the data as a Task.
In the OnSuccessListener of the Task, extract the data from the DocumentSnapshot as a Map.
Iterate over the Map and create instances of your POJO class from the data.
Here’s an example code snippet: