Firebase查询数据 [英] Firebase querying data

查看:104
本文介绍了Firebase查询数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
random_key 1:{
id:0,
text:This is text
},
random_key 2:{
id:1,
text:这是文本
}
}

如果我以这种方式存储数据,并且想获取 id 等于 0 。上面是问题的子元素,它是<$ c的子元素。

解决方案

在你的情况下,你将不得不设置一个像这样的查询:

  DatabaseReference reference = FirebaseDatabase.getInstance()。getReference(); 

Query query = reference.child(issue)。orderByChild(id)。equalTo(0);
query.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.exists()){
// dataSnapshot是问题节点,所有的孩子的ID为0
(DataSnapshot问题:dataSnapshot.getChildren()){
//对单个问题做些事



$ b @Override
public void onCancelled(DatabaseError databaseError){
$ b}
});


{
  "random_key 1" : {
    "id": 0,
    "text": "This is text"
  },
  "random_key 2" : {
    "id": 1,
    "text": "This is text"
  }
}

If I'm storing my data like this, and I want to get the node where id is equal to 0. How can I do that?

The above is the child of issue, which is a child of root.

解决方案

In your case you would have to setup a Query like this:

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

    Query query = reference.child("issue").orderByChild("id").equalTo(0);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                // dataSnapshot is the "issue" node with all children with id 0
                for (DataSnapshot issue : dataSnapshot.getChildren()) {
                    // do something with the individual "issues"
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

这篇关于Firebase查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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