3

I am having trouble retrieving hash map values from Firestore. I have no trouble in uploading the values, but I am having difficulty in retrieving them. Here is my Firestore database.

Here are my constructors:

public class OrderItems {
    private String uid;
    private String product;
    private int qty;
    private int price;
    private int totalPrice;

    public OrderItems(){

    }

    public OrderItems(String uid, String product, int qty, int price) {
        this.uid = uid;
        this.product = product;
        this.qty = qty;
        this.price = price;
    }

    public String getUid() {
        return uid;
    }

    public String getProduct() {
        return product;
    }

    public int getQty() {
        return qty;
    }

    public int getPrice() {
        return price;
    }

    public int getTotalPrice() {
        return qty * price;
    }

    public void setTotalPrice(int totalPrice) {
        this.totalPrice = totalPrice;
    }
}

My hash map constructor:

public class OrderObject {

    private HashMap<String, OrderItems> orders;

    public OrderObject(){

    }

    public OrderObject(HashMap<String, OrderItems> orders) {
        this.orders = orders;
    }

    public HashMap<String, OrderItems> getOrders() {
        return orders;
    }
}

How do I store the objects using a constructor through an array list? And how can I access the individual fields afterwards? Thank you!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Viorii
  • 31
  • 1
  • 4

2 Answers2

3

You need to cast your DocumentSnapshot to HashMap

db.collection("Deliveries").document("your_document_id").get()
.addOnSuccessListener {
val obj = it.get("Coconut") as HashMap<*, *>
Log.d("TAG",   it.get("Coconut").toString())
// This will print the whole map value
Log.d("TAG", obj.get("qty").toString())
}.addOnFailureListener {}
Hritik Gupta
  • 611
  • 5
  • 20
2

It looks like you are storing OrderItems objects within a single document. If you attach a listener on a particular object, the object that is returned is of type DocumentSnapshot:

A DocumentSnapshot contains data read from a document in your Firestore database. The data can be extracted with the getData() or get() methods.

If you intend to use getData(), a Map object is returned, so you can simply iterate over it and get the desired data. But remember, you cannot get directly a list of OrderItems objects, you need to write code for that.

I'm not sure what you want to achieve, but in your case, a more appropriate solution would be to store those items as separate documents in a collection. In this way you can get all documents (OrderItems objects) within an order collection and display them in a RecyclerView, as explained in my answer from the following post:

Edit:

If you need to map an array of custom objects into a list, please check the following article:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • StackOverflow community should remove this answer because author of this answer has posted link to PAID article, not an answer. This is not the answer. – Pooja Aug 17 '23 at 14:38