简体   繁体   English

FirebaseDatabase.getInstance()。getReference()到子节点并列出该子节点下的键值对

[英]FirebaseDatabase.getInstance().getReference() to child node and list key value pairs under that child

I am trying to access the shrubs child and under each unique key under shrubs, access the key value pairs eg key : ShrubbedWord value : yfyj 我正在尝试访问灌木子,并在灌木下的每个唯一键下访问键值对,例如keyShrubbedWord valueyfyj

单击此处查看我的Firebase树结构的屏幕截图

I've tried this but I get nothing:- 我已经尝试过了,但是什么也没得到:

postsData = FirebaseDatabase.getInstance().getReference().child("Shrubs");

        ChildEventListener childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    for (DataSnapshot dttSnapshot2 : dataSnapshot.getChildren()) {
                            Log.d("mylog", "" + dttSnapshot2.getKey().toString() + ":" + dttSnapshot2.getValue().toString());
                    }
            }

Now when I try this: 现在,当我尝试这个:

postsData = FirebaseDatabase.getInstance().getReference();

        ChildEventListener childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    for (DataSnapshot dttSnapshot2 : dataSnapshot.getChildren()) {
                            Log.d("mylog", "" + dttSnapshot2.getKey().toString() + ":" + dttSnapshot2.getValue().toString());
                    }
            }

My log returns a whole bunch of data from birthday all the way to Shrubs which I don't need. 我的日志从生日一直返回到我不需要的灌木丛中的全部数据。 Just want the data in the Shrubs child in key value pairs. 只希望“灌木”子项中的数据具有键值对。

Anyone know how I can achieve this? 有人知道我能做到这一点吗?

Try this: 尝试这个:

 DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("unknown").child("Shrubs");
ref.addValueEventListener(new ValueEventListener(){
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnasphot datas: dataSnapshot.getChildren()){
     String name=datas.child("ShrubbedWord").getValue().toString();
    } 
 }

@Override
public void onCancelled(FirebaseError firebaseError) {


    }
   });

You need to go from top to bottom, in the image that you provided there is a child node that you added red lines on it so we do not see it. 您需要从上到下进行操作,在提供的图像中,有一个子节点,在该子节点上添加了红线,因此我们看不到它。

I have named it in the code as child("unknown") , but in your code name it as the name that it is written in the database. 我在代码中将其命名为child("unknown") ,但在您的代码中将其命名为它在数据库中写入的名称。

After that you write child("Shrubs") , then your dataSnapshot will be at the location of the Shrubs child node. 之后,编写child("Shrubs") ,然后dataSnapshot将位于child("Shrubs")子节点的位置。 Then you can iterate inside the push id to be able to retrieve the attributes and the values inside of it. 然后,您可以在推送ID中进行迭代,以便能够检索其中的属性和值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM