Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help in retrieving hash map values from Firestore (Android)

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!

like image 627
Viorii Avatar asked Mar 12 '26 12:03

Viorii


1 Answers

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 {}
like image 66
Hritik Gupta Avatar answered Mar 15 '26 02:03

Hritik Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!