I am saving items to firebase and I get the following structure
I am using the following code to try to retrieve the descripcion and precio data that is inside the second child
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));
}
}
But it marks me a null because it can't find them
You have two nested levels with dynamic keys in your JSON, so you need two nested loops over getChildren()
in your onDataChange
:
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){
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.