5

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?

adjuremods
  • 2,938
  • 2
  • 12
  • 17
august alsina
  • 197
  • 2
  • 4
  • 15

3 Answers3

13

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();
fsebek
  • 389
  • 2
  • 9
5

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.

Zura Sekhniashvili
  • 1,147
  • 7
  • 19
0

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) {

        }
    });