简体   繁体   中英

get user node from Firebase using the uid in Android?

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

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

The JSON is here

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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