我有ởFirebase数据库结构如下所示。 enter image description here 我想从红色区域获取数据。我试过这段代码:
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);
}
}
}
收据temp2返回null。请有人告诉我如何获取这些数据。提前致谢 编辑1: 这是我的收据类:
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;
}
}
答案 0 :(得分:1)
您的数据结构似乎是:
orders
$uid
$orderid
...
您的代码会读取所有/orders
,然后遍历结果。这意味着你正在循环用户。所以至少你错过了代码中的循环。
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);
}
}
}
...