Get child name from Firebase?

六月ゝ 毕业季﹏ 提交于 2019-12-23 10:06:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!