How to retrieve data from one single userID Firebase Android

两盒软妹~` 提交于 2019-12-05 22:02:33
Mufad

First initialize your database like this

mDatabase= FirebaseDatabase.getInstance().getReference().child("your_node_name");

then try setting a ValueEventListener to your Database reference. Inside the onDataChange method you can retrieve your data like this

public void onDataChange(DataSnapshot dataSnapshot) {
    User user= dataSnapshot.getValue(User.class);
}

EDIT:

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User user= dataSnapshot.getValue(User.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};
mDatabase.addValueEventListener(postListener);
  DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("UsersTable").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists())
        {
            for(DataSnapshot userDetails : dataSnapshot.getChildren()) {
                Log.d("valueName:", userDetails.child("Name").getValue());
                Log.d("valueEmail:", userDetails.child("Email").getValue());
                Log.d("valueuserid:", userDetails.child("userid").getValue());
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

use this will solve your problem.

Add value event listener instead of child event listener.

df.child("UsersTable").child(user.getUid()).addValueEventListener(newValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           if(dataSnapshot!=null){
             fetch(dataSnapshot);
           }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

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