如何从Android中的Firebase实时数据库中获取数据 [英] How to fetch data from firebase real time database in android
本文介绍了如何从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) {
}
});
这将为您提供msgContent
和msgType
,要从数据库中检索任何数据,您需要遍历每个节点的示例:-
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.
这篇关于如何从Android中的Firebase实时数据库中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文