根据键从firebase中获取值

时间:2018-03-15 18:01:07

标签: java android firebase firebase-realtime-database

这是数据库的结构

enter image description here

我需要获取" QNGGvYL7dgde9Oypi9a4IFdAys03"

的所有值

我已经尝试过这段代码,但它无效

    String uid = FirebaseAuth.getInstance().getUid();
    DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference();
    Query query = databaseRef.child("bookmarks").equalTo(FirebaseAuth.getInstance().getUid());
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
               vehicleList.add(postSnapshot.getValue().toString());
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

1 个答案:

答案 0 :(得分:1)

如果您知道要获得的密钥,则不需要查询:

DatabaseReference ref = databaseRef.child("bookmarks").child(FirebaseAuth.getInstance().getUid());
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
           vehicleList.add(postSnapshot.getValue().toString());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errorrs
    }
});