简体   繁体   English

使用Android中的uid从Firebase获取用户节点?

[英]get user node from Firebase using the uid in Android?

How can I get the user node using uid generated through: 如何使用通过以下方式生成的uid获取用户节点:

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

The JSON is here JSON在这里

You will need to create a POJO object class in order to get the atributes from that current uid user 您将需要创建一个POJO对象类,以便从该当前uid用户获取属性。

to do that first create a class with your user variables, i will call this UserPojo.class 为此,首先使用您的用户变量创建一个类,我将其称为UserPojo.class

public class userPojo {

 private String email;
    private String id;
    private String name;


    public userPojo() {

    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

 }

And then just iterate through that node and get your values this way 然后遍历该节点并以这种方式获取您的值

first declare your database reference 首先声明您的数据库引用

private DatabaseReference mDatabase;

then inside onCreate() 然后在onCreate()

mDatabase = FirebaseDatabase.getInstance().getReference(); //root reference to the database

then just get the values inside the reference 然后只获取参考内的值

 mDatabase.child("collion").child(uid).addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot snapShot : dataSnapshot.getChildren()){
        UserPojo user = snapShot.getValue(UserPojo.class);
        //get your values inside that uid
         String name = polla.geName();
         String email = polla.getEmail();
         String id = polla.getId();

          Log.e("Data: " , "" + name + "" + email+""+id);

           }


      }

      @Override
      public void onCancelled(DatabaseError databaseError) {
        System.out.println("The read failed: " + databaseError.getCode());
      }
    });

thats all, any question feel free to ask 多数民众赞成在任何问题上随时问

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. 我正在根据您的请求从此处编写此答案,但我看到尽管您从FirebaseUser对象获取了uid ,但您根本没有使用它。 You are still using that random unique key provided by the push() method. 您仍在使用push()方法提供的随机唯一键。 So if you want to use the uid , your database structure should look like this: 因此,如果要使用uid ,则数据库结构应如下所示:

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

See, I have used the uid that is coming as a result from the following line of code: 看到了,我已经使用了以下代码行所产生的uid

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

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. 第一个是@GastónSaillén在回答中使用模型类解释的,或者使用String类以更简单的方式解释。 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);

The result in your logcat will be: fawad . 您的logcat中的结果将是: fawad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM