简体   繁体   English

Firebase DataSnapshot,无法检索正确的子项列表

[英]Firebase DataSnapshot, Can't retrieve the right list of children

I have a database structure for chatting application so i have a message node in database looks like this我有一个用于聊天应用程序的数据库结构,所以我在数据库中有一个消息节点,如下所示在此处输入图片说明

messages is parent of a key the contains the UIDs of the two users chatting and this key contains the value of the chat body, I the application is working so far so good when pushing data done as i want.消息是一个键的父键,它包含两个聊天用户的 UID,这个键包含聊天正文的值,到目前为止,我的应用程序在按我想要的方式推送数据时工作得很好。

But when trying to read the data the DataSnapShot object always have the the last leaf of the node means the child of the key.但是当试图读取数据时,DataSnapShot 对象总是具有节点的最后一片叶子,即键的子节点。 I tried different approaches to get the key it self to iterate through and get a list of its values with no luck.我尝试了不同的方法来获取它自己迭代的密钥,并在没有运气的情况下获得它的值列表。

Approach i tried我试过的方法

  public static final String MESSAGES_NODE_DB = "messages"; 
  mMessageDbRef = FirebaseDatabase.getInstance().getReference()
                    .child(MESSAGES_NODE_DB).child(keyUserA + "-" + KeyUserB);
    
     mMessageDbRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    Timber.d("parent key " + dataSnapshot.getKey());
    
                    for (DataSnapshot singleSnap : dataSnapshot.getChildren()) {
                            Message message = singleSnap.getValue(Message.class);
                            messageList.add(message);
                            instantiateRecyclerView();
                    }
                }

This approach always cause an error Can't convert object of type这种方法总是会导致错误 Can't convert object of type

java.lang.String字符串

And parent key prints L91eMRVq_nx6WHUzZFo并且父密钥打印 L91eMRVq_nx6WHUzZFo

Another one另一个

    mMessageDbRef = FirebaseDatabase.getInstance().getReference().child("messages");
for (DataSnapshot singleSnap : dataSnapshot.getChildren()) {
                Timber.d("keys" + singleSnap.getKey());
            }
        }

The output is L91eMRVq_nx6WHUzZFo输出为 L91eMRVq_nx6WHUzZFo

What i want to achieve我想要达到的目标

To retrieve the list of keys inside of messages node as检索消息节点内的键列表为

vXgRtbjqhUYPOLyjhmGKRzITUC83-24L0kQx75fhoz06YpXnWRheETct2 vXgRtbjqhUYPOLyjhmGKRzITUC83-24L0kQx75fhoz06YpXnWRheETct2

Inside onChildAdded method do not use getChildren() and the for loop method as the onChildAdded method returns the individual nodes below the mMessageDbRef reference.在 onChildAdded 方法内部不使用 getChildren() 和 for 循环方法,因为 onChildAdded 方法返回 mMessageDbRef 引用下方的各个节点。 Basically the data snapshot parameter in onChildAdded can be converted to a Message object directly.基本上 onChildAdded 中的数据快照参数可以直接转换为 Message 对象。

Thus, directly assign the data snapshot value to the message object as you have done without using any loop or the getChildren method.因此,直接将数据快照值分配给消息对象,就像您所做的那样,无需使用任何循环或 getChildren 方法。

And by doing this you will have access to the value of the message through the message object variables.通过这样做,您将可以通过消息对象变量访问消息的值。

Something like this:像这样的东西:

@Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                        Message message = dataSnapshot.getValue(Message.class);
                        messageList.add(message);
                        instantiateRecyclerView();
                }
            }

To retrieve the key inside the messages node:要检索messages节点内的密钥:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("messages").child(keyUserA + "-" + KeyUserB);

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
   String keys=datas.getKey();
    }
 }
 @Override
public void onCancelled(DatabaseError databaseError) {
   }
 });

the datasnapshot is on child(keyUserA + "-" + KeyUserB);数据快照位于child(keyUserA + "-" + KeyUserB); then you iterate inside the randomid L91eMRVq_nx6WHUzZFo and you will be able to retrieve it using getKey()然后您在 randomid L91eMRVq_nx6WHUzZFo迭代,您将能够使用getKey()检索它

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

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