Home > Software engineering >  How to read map of nested custom objects from Firestore in Android
How to read map of nested custom objects from Firestore in Android

Time:02-04

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 enter image description here

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.

CodePudding user response:

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:

class Test {
    String field2, field1, field3;

    public Test(String field1, String field2, String field3) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
    }
}

And right after that, please use the following lines of code:

FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference docRef = db.collection("collname").document("ABCD");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document != null) {
                List<Test> testList = new ArrayList<>();
                Map<String, Object> data = document.getData();
                data.values().forEach( h -> {
                            Map<String, String> map = (Map<String, String>) h;
                            testList.add(new Test(map.get("field1"), map.get("field2"), map.get("field3")));
                        }
                );
                Log.d(TAG, "size: "   testList.size());
            } else {
                Log.d("TAG", "User doesn't exist.");
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

According to your screenshot, the result in the logcat will be:

2

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:

CodePudding user response:

o 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:

FirebaseFirestore db = FirebaseFirestore.getInstance(); DocumentReference docRef = db.collection("collectionName").document("documentName"); docRef.get().addOnSuccessListener(new OnSuccessListener

  • Related