Android Firebase getting child values

I have ở Firebase Database structure looks like this. enter image description here I want to get the data from red area. I have tried this code:

ref.child("shop").child("orders").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.getValue() != null) {
                    for (DataSnapshot temp : dataSnapshot.getChildren()) {
                        Receipt temp2 = temp.getValue(Receipt.class);
                    }
                }
            }

The Receipt temp2 return null. Please someone can tell me how i can get that data. Thanks in advance Edit 1: Here is my Receipt class:

public class Receipt {
    private String name;
    private String province;
    private String district;
    private String address;
    private String phoneNumber;
    private String style_payment;
    private int state;
    private int tmp;
    private int cost;
    private int total;
    private ArrayList<ProductInReceipt> productList;

    public Receipt(){}
    public Receipt(String name, String province, String district, String address, String phoneNumber, int state, ArrayList<ProductInReceipt> productList,String style_payment,
                   int tmp,int cost, int total) {
        this.name = name;
        this.province = province;
        this.district = district;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.state = state;
        this.productList = productList;
        this.style_payment = style_payment;
        this.tmp = tmp;
        this.cost = cost;
        this.total = total;
    }
}

Upvotes: 0

Views: 52

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

It seems like your data structure is:

orders
  $uid
    $orderid
      ...

Your code reads all of /orders and then loops over the results. That means that you're looping over users. So at the very least you're missing a loop in your code.

ref.child("shop").child("orders").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
      for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
        for (DataSnapshot receiptSnapshot: dataSnapshot.getChildren()) {
            Receipt receipt = receiptSnapshot.getValue(Receipt.class);
        }
      }
    }
    ...

Upvotes: 1

Related Questions