I have list of operations and each operation has a id. So how can I read it without knowing id?
The data looks like this:
I tried to write code but it doesn’t work.
myRef.child("Анна Валерьевна").child("operations").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String s = "";
for (DataSnapshot ds : snapshot.getChildren()){
if (snapshot.child("name").getValue(String.class) != null) {
s = snapshot.child("name").getValue(String.class);
}
adapter.add(s);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
>Solution :
Your code tries to read snapshot.child("name")
, but the loop variable ds
. So that should be ds.child("name")
.
myRef.child("Анна Валерьевна").child("operations").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String s = "";
for (DataSnapshot ds : snapshot.getChildren()){
if (ds.child("name").getValue(String.class) != null) {
s = ds.child("name").getValue(String.class);
}
adapter.add(s);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});