(https://i.stack.imgur.com/qXm2W.png)
我想获得产品ID,产品名称,数量等的价值,我怎样才能获得所有这些价值
答案 0 :(得分:0)
如果您想从单个食物对象(0
)获取该数据,请使用以下代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference zeroRef = rootRef.child("Request").child(uid).child("foods").child("0");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String discount = dataSnapshot.child("discount").getValue(String.class);
String price = dataSnapshot.child("price").getValue(String.class);
String productId = dataSnapshot.child("productId").getValue(String.class);
String productName = dataSnapshot.child("productName").getValue(String.class);
String quantity = dataSnapshot.child("quantity").getValue(String.class);
Log.d("TAG", discount + " / " +
price + " / " +
productId + " / " +
productName + " / " +
quantity);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
zeroRef.addListenerForSingleValueEvent(eventListener);
如果您将拥有更多单个对象,请使用以下代码:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference foodsRef = rootRef.child("Request").child(uid).child("foods");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String discount = ds.child("discount").getValue(String.class);
String price = ds.child("price").getValue(String.class);
String productId = ds.child("productId").getValue(String.class);
String productName = ds.child("productName").getValue(String.class);
String quantity = ds.child("quantity").getValue(String.class);
Log.d("TAG", discount + " / " +
price + " / " +
productId + " / " +
productName + " / " +
quantity);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
foodsRef.addListenerForSingleValueEvent(eventListener);
假设Request
节点是您的Firebase根的直接子节点,并且其下的第一个直接子节点是用户ID。
修改强>
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference requestRef = rootRef.child("Request");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String discount = ds.child("foods").child("0").child("discount").getValue(String.class);
String price = ds.child("foods").child("0").child("price").getValue(String.class);
String productId = ds.child("foods").child("0").child("productId").getValue(String.class);
String productName = ds.child("foods").child("0").child("productName").getValue(String.class);
String quantity = ds.child("foods").child("0").child("quantity").getValue(String.class);
Log.d("TAG", discount + " / " +
price + " / " +
productId + " / " +
productName + " / " +
quantity);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
requestRef.addListenerForSingleValueEvent(valueEventListener);