从Firebase检索具有特定ID的数据 [英] Retrieving Data from Firebase with certain ID

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

问题描述

我有一个用户ID,我想从火力中取他的名字.因此,我尝试使用orderByKey()方法来查找某个用户,然后,从其个人资料中获取信息.但是出了点问题...

I have an ID of User, and i want to take his name from the firebase. So, i am trying to use orderByKey() method to find a certain user and after that, take information from his profile. But something goes wrong ...

reference = FirebaseDatabase.getInstance().getReference("Users");
    Query query = reference.orderByKey().equalTo(task.author_id);
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            fullName = dataSnapshot.getValue(User.class).getFullName();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Firebase结构

推荐答案

在这种情况下,如果您已经知道该节点的特定路径,则不需要查询.只需直接添加侦听器即可.

You don't need a query in that case if you already know the specific path to that node. Just add the listener directly.

reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(task.author_id).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        fullName = dataSnapshot.getValue(User.class).getFullName();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

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

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