问题
If I have something like:
child name: "value"
How can I get the childname? I know its possible to get the value, but what about the other?
回答1:
The child name in this situation is the "KEY" of that value. So use the reference object and call getKey() on it.
String name = ref.getKey();
回答2:
Try this.
var ref = firebase.database().ref("root/childName");
ref.once("value")
.then(function(snapshot) {
var key = snapshot.key; // "childName";
});
[UPDATED]
Sorry I posted for javascript.
回答3:
get all child:
dbRef.child("tipo_arte").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
item.clear();
item.add("");
for (DataSnapshot child : dataSnapshot.getChildren()) {
item.add(child.getKey()); //Pega o nome de cada tipo de arte
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
来源:https://stackoverflow.com/questions/40101010/get-child-name-from-firebase