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 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!
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 {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With