如何从Android中的Firebase实时数据库中获取数据

如何从Android中的Firebase实时数据库中获取数据

问题描述:

我必须从Firebase实时数据库中获取所有子级数据吗?我想获取User2收到的消息.怎么做?数据库示例如下-

I have to fetch all children data from firebase real time database? I want to fetch received messages of User2. How to do that? Database example is as given below -

要获取子数据,请尝试以下操作:

To be able to fetch the children data, try the following:

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users").child("User1").child("MSG_TYPE_SENT");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshot datas: dataSnapshot.getChildren()){
     String msgContent=datas.child("msgContent").getValue().toString();
     String msgType=datas.child("msgType").getValue().toString();
    }
  }
 @Override
public void onCancelled(DatabaseError databaseError) {
    }
 });

这将为您提供msgContentmsgType,要从数据库中检索任何数据,您需要遍历每个节点的示例:-

This will give you the msgContent and the msgType, to retrieve any data from the database, you need to pass through every node example:-

getReference("users").child("User1").child("MSG_TYPE_SENT");

然后,您可以在ID内循环并检索数据.

Then you can loop inside the ids and retrieve the data.