get user node from Firebase using the uid in Android?

醉酒当歌 提交于 2019-12-24 09:07:30

问题


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

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

The JSON is here


回答1:


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"

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

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. 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.




回答2:


You will need to create a POJO object class in order to get the atributes from that current uid user

to do that first create a class with your user variables, i will call this 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()

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



来源:https://stackoverflow.com/questions/51349364/get-user-node-from-firebase-using-the-uid-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!