以下是Firebase中存储的内容:
"73668" : {
"id" : "73668",
"price" : "Price",
"product1" : "1. Product 1",
"product2" : "2. Product 2",
"product3" : "3. Product 3",
"status" : "not ready"
},
"a2521" : {
"id" : "a2521",
"price" : "Price",
"product1" : "1. Product 1",
"product2" : "2. Product 2",
"product3" : "3. Product 3",
"status" : "not ready"
},
"a719b" : {
"id" : "a719b",
"price" : "Price",
"product1" : "1. Product 4",
"product2" : "2. Product 5",
"product3" : "3. Product 6",
"status" : "not ready"
}
}
我正在使用的代码:
public ArrayList<Order> getOrders() {
final Firebase myFirebaseRef = new Firebase("https://torrid-heat-2650.firebaseio.com/orders");
myFirebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
Order order = new Order();
order.setProduct1(child.child("product1").getValue().toString());
order.setProduct2(child.child("product2").getValue().toString());
order.setProduct3(child.child("product3").getValue().toString());
order.setId(child.child("id").getValue().toString());
order.setPrice(child.child("price").getValue().toString());
itemsList.add(order);
}
}
public void onCancelled(FirebaseError firebaseError) {
}
});
System.out.println(itemsList.size());
return itemsList;
}
相同的操作会在Android设备上返回空的arraylist。有趣的是,它在两种情况下都输出0,但是返回非空的arraylist。什么可能导致相同代码中的2个不同输出?谢谢。