获取持有另一个子子节点的子节点的名称

时间:2018-03-09 15:05:37

标签: android firebase firebase-realtime-database

Child1
     Child1.1
              Subchild1
              Subchild 2
     Child 1.2
               Subchild 1
               Subchild 2

如果我需要检索child1.1和1.2如何在textview中获取它们?,因为可以使用datasnapshot轻松检索它们的子项。

我没有发布图像的声誉,所以必须输入我的firebase项目的结构。希望我的问题是可以理解的。 代码我做的是......

LoggedInRef.child(number).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot != null) {
                    if (sampleList != null) {

                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                  /*  msg_List_Item msgLIST=ds.getValue(msg_List_Item.class);

                        msgList.add(msgLIST);*/
                            Log.d("MESSAGES<<<<<<<<", ds.getValue().toString() + "<<<<<<<<<<<<<<<<<<<<<<<<<<");
                            String msg = ds.getValue(String.class);              // problem is here............................................
                            String time = ds.child("DateTime").getValue(String.class);
                            String tag = ds.child("Tag").getValue(String.class);
                            msg_List_Item msg_list_item = new msg_List_Item(msg, time, tag);
                      /* msg_list_item.setMsg(msg);
                       msg_list_item.setDateTime(time);
                       msg_list_item.setTag(tag);*/

                            msgList.add(msg_list_item);


                            MessageAdapter = new msgAdapter(msgList, getApplicationContext());
                            mRecyclerView.setAdapter(MessageAdapter);
                            sampleList.clear();
                            mRecyclerView.scrollToPosition(MessageAdapter.getItemCount());
                        }
                    }
                }
                else {
                        Toast.makeText(Message.this, "No data  found", Toast.LENGTH_SHORT).show();   
                    }}

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }}
        );

1 个答案:

答案 0 :(得分:0)

"1234567890" : {
    "-L7AhB1GWZPNqlte0tiX" : {
      "DateTime" : "09-03-2018 22:30",
      "MSG" : "hiii ",
      "Tag" : "ByMe"
    },
    "-L7AiOxVbSYAx8_0WqEa" : {
      "DateTime" : "09-03-2018 22:35",
      "MSG" : "how are you",
      "Tag" : "ByMe"
    },
    "-L7Ak1lhqn9EfD5YCOou" : {
      "DateTime" : "09-03-2018 22:42",
      "MSG" : "what are u doing?",
      "Tag" : "ByMe"
    },
    "-L7AkGekOzKzlLOwWNWp" : {
      "DateTime" : "09-03-2018 22:43",
      "MSG" : "I am fine",
      "Tag" : "NotByMe"

这是我的Re修改结构!!! *子节点:* &#34;日期时间&#34;,&#34; MSG&#34;,&#34;标记&#34; 变得更容易获取使用 push()