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

问题描述

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

如果我像这样存储我的数据,并且我想获得 id 等于 0 的节点.我该怎么做?

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?

上面是issue的孩子,是root的孩子.

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 查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:01