使用Android中的uid从Firebase获取用户节点? [英] get user node from Firebase using the uid in Android?

查看:66
本文介绍了使用Android中的uid从Firebase获取用户节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用通过以下方式生成的uid获取用户节点:

How can I get the user node using uid generated through:

     String uid=
     FirebaseAuth.getInstance().getCurrentUser().getUid();

JSON在此处

推荐答案

我正在根据您从

I'm writing this answer according to your request from here but I see that although you are getting the uid from the FirebaseUser object, you are not using it at all. You are still using that random unique key provided by the push() method. So if you want to use the uid, your database structure should look like this:

Firebase-root
   |
   --- users
         |
         --- uid
              |
              --- email: "nicefawad1@gmail.com"
              |
              --- uid: "uid"
              |
              --- name: "fawad"

看,我已经使用了以下代码行产生的uid:

See, I have used the uid that is coming as a result from the following line of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

为了读取此数据,您有两个选择.第一个是@GastónSaillén在回答中使用模型类解释的,或者使用String类以更简单的方式进行解释.因此,要获取特定用户的名称,请使用以下代码行:

In order to read this data, you have two options. The first one would be as @Gastón Saillén explained in his answer using a model class, or in a more simpler way using the String class. So to get the name of a specific user, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("name").getValue(String.class);
        Log.d("TAG", name);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

您的logcat中的结果将是:fawad.

The result in your logcat will be: fawad.

这篇关于使用Android中的uid从Firebase获取用户节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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