'How to use use Firestore document field's HashMap key value and populate recyclerview?

I have just started learning android. I want to know is a way by which I can populate a RecyclerView using a firebase document's field which contains only the HashMap key and value. Sorry if I asked the wrong question. I just want to know if it is possible?this is the snapshot of my firebase

Please note- I know how to store the items (that I am storing in the map) as separate documents in a collection and make RecyclerView. Currently, I am using this method only and populating the RecyclerView. But I don't want to store the items as a separate documents.

This is the required output needed...Required reyclerview

public class ModelTrans{
    Double RecentAmount;
    String TransText;
    String Transactionid;
    @ServerTimestamp
    Date date;
    int img;

    public ModelTrans() {
    }

    public ModelTrans(Double recentAmount, String transText, String transactionid, Date date, int img) {
        RecentAmount = recentAmount;
        Transtext = transText;
        Transactionid = transactionid;
        this.date = date;
        this.img = img;
    }

......other getter setters

}



Solution 1:[1]

Since you say that you have added those objects into a collection and it worked, you can do the same thing in a document as well. What you have to do is to create a reference that points to that particular document and then read it.

Each field of the document is an object of type ModelTrans. To get that data you should use the following lines of code:

joinedContestRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                List<ModelTrans> transList = new ArrayList<>();
                Map<String, Object> map = document.getData();
                for(Map.Entry<String, Integer> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Map<String, Object> value = (Map<String, Object>) entry.getValue();
                    //Iterate again and add the data to a transList.
                }
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

As you can see, I have used DocumentSnapshot#getData() method that returns an object of type Map<String, Object>. What you have to do now, is to iterate through the map and get the data inside it. You actually only need the values. In the end, simply add those values to a List, and then pass that List to an adapter. And that should be pretty much of it.

Edit:

Good point in your comment. I should have been added it from the beginning.

Yes, there are some limits when it comes to how much data you can put into a single document. According to the official documentation regarding usage and limits:

Maximum size for a document: 1 MiB (1,048,576 bytes)

As you can see, you are limited to 1 MiB total of data in a single document. So be aware of this limitation, otherwise, your writes may start to fail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1