[英]How can I retrieve this element in firebase realtime?
我將項目保存到 firebase 並得到以下結構
我正在使用以下代碼嘗試檢索第二個孩子內部的描述和精確數據
DatabaseReference itemFactura = FirebaseDatabase.getInstance().getReference().child("factura");
itemFactura.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
int cantidad = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
if(Boolean.parseBoolean(snapshot.child("estado").getValue().toString())){
ArrayList<String> elementos = new ArrayList<String>();
elementos.add(snapshot.child("descripcion").getValue().toString());
elementos.add(snapshot.child("precio").getValue().toString());
tabla.agregarFilaTabla(elementos);
cantidad = Integer.parseInt(snapshot.child("precio").getValue().toString()) + cantidad;
}
}
cantidadTotal.setText(String.valueOf(cantidad));
}
}
但它把我標記為 null 因為它找不到它們
您在 JSON 中有兩個帶有動態鍵的嵌套級別,因此您需要在onDataChange
中的getChildren()
上進行兩個嵌套循環:
DatabaseReference itemFactura = FirebaseDatabase.getInstance().getReference().child("factura");
itemFactura.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
int cantidad = 0;
for (DataSnapshot level1snapshot : dataSnapshot.getChildren()){
for (DataSnapshot level2snapshot : level1snapshot.getChildren()){
if(level2snapshot.child("estado").getValue(Boolean.class) != null){
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.