Firebase查询数据

时间:2016-08-18 17:58:50

标签: android firebase firebase-realtime-database

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

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

以上是issue的孩子,root的孩子。

4 个答案:

答案 0 :(得分:35)

在您的情况下,您必须设置如下查询:

    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) {

        }
    });

答案 1 :(得分:3)

@ Linxy的答案是正确的,但由于您将从数据库中读取项目列表,因此最好使用子事件侦听器而不是值事件侦听器。

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

Query query = reference.child("issue").orderByChild("id").equalTo(0);
query.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        //Do something with the individual node here`enter code here`
    }

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

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

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

    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }


});

答案 2 :(得分:1)

此代码适用于我

mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(final DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                 //If email exists then toast shows else store the data on new key
                    if (!data.getValue(User.class).getEmail().equals(email)) {
                        mFirebaseDatabase.child(mFirebaseDatabase.push().getKey()).setValue(new User(name, email));
                    } else {
                        Toast.makeText(ChatListActivity.this, "E-mail already exists.", Toast.LENGTH_SHORT).show();
                    }
                }
            }

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

答案 3 :(得分:0)

对于易于使用的Firebase跨平台集成,您还可以查看V-Play Engine for mobile apps

FirebaseDatabase {
  id: firebaseDb

  Component.onCompleted: {
    //use query parameter:
    firebaseDb.getValue("public/bigqueryobject", {
                   orderByKey: true,  //order by key before limiting
                   startAt: "c",      //return only keys alphabetically after "c"
                   endAt: "m",        //return only keys alphabetically before "m"
                   limitToFirst: 5,   //return only first 5 sub-keys
    })
  }
}