I need a function that has two parameters, (UserID and child key) it should return the value of child from firebase. I want something like this
public String getValue("user1", "Age")
{
//something to do
return age;
}
It should return value of the child.
This is how i'm doing:
private String getValue(String parent, final String child) {
final String[] val = new String[1];
database.getReference(parent).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String key = snapshot.getKey();
String value = ""+snapshot.getValue(String.class);
if(key.equals(child))
{
val[0] = value;
break;
}
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
return val[0];
}
This is how my database looks like there: