0

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;
    }
}
  • 1
    Possible duplicate of [How to get all child's data in firebase database?](https://stackoverflow.com/questions/38965731/how-to-get-all-childs-data-in-firebase-database) –  May 05 '18 at 16:07
  • can you post your Receipt class code? – iamkdblue May 05 '18 at 16:10

1 Answers1

1

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);
        }
      }
    }
    ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807