如何访问Firebase数据库中嵌套在子节点中的子节点? [英] How do I access the child nodes nested inside child node in Firebase database?

查看:54
本文介绍了如何访问Firebase数据库中嵌套在子节点中的子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在firebase中的数据库结构.

Here is my database structure in firebase.

我想访问message child下的消息内容.我可以获取message child的子级计数,但是无法检索每个child的值.

I want to access the message content under messages child.I can get the children count of the messages child ,but cannot retrieve the value of each child.

ref2指向我的Firebase实例.
我必须从参与聊天室中获取所有消息.

ref2 points to my firebase instance.
I have to get all the messages from the particulat chatroom.

     ref2.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
                        listText.add(childDataSnapshot.child("messages").child("message").getValue() + "");
                        listText.add(childDataSnapshot.child("messages").child("name").getValue() + "");
                        //   userArrayList.add(new User("" + childDataSnapshot.child("username").getValue(), "" + childDataSnapshot.child("profileURL").getValue(), ""));
                        Log.v("messag_ids", "" + childDataSnapshot.child("messages").child("Kd4TQPrAWsNY1vcid1P").child("message")); //displays the key for the node

                    }
                    lvChat.setAdapter(new ChatAdapter(ChatActivity.this, listUsername, listText));
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

推荐答案

我有一个更简单的答案:

I have more simple answer :

您将尝试访问消息节点,就像其他节点(如聊天,所有者等)一样... //Ur数据库引用是父节点,当您尝试作为子节点访问消息时,请使用以下代码:

You will try to access the message node like other nodes like chatpic, owner etc ... // Ur Database reference is the parent node , when u are trying to access message as the child use the below code :

for(DataSnapshot dataSnapshot1:mainSnapShot.child("message").getChildren()){                          

                            Map<?, ?> valuex = (Map<?, ?>) dataSnapshot1.getValue();


                            if (!TextUtils.isEmpty(valuex.get("message").toString())) {
                                Log.i("message",valuex.get("message").toString());
                            }



                            if (!TextUtils.isEmpty(valuex.get("name").toString())) {
                                Log.i("name",valuex.get("name").toString());
                            }

                        }

这篇关于如何访问Firebase数据库中嵌套在子节点中的子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆