mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference().child("Users");
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
toastMessage("Successfully signed out.");
}
// ...
}
};
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
/*private void showData(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
UserInformation uInfo = new UserInformation();
uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName()); //set the name
uInfo.setHandicap(ds.child(userID).getValue(UserInformation.class).getHandicap()); //set the name
uInfo.setAge(ds.child(userID).getValue(UserInformation.class).getAge()); //set the email
uInfo.setGender(ds.child(userID).getValue(UserInformation.class).getGender()); //set the phone_num
//display all the information
Log.d(TAG, "showData: name: " + uInfo.getName());
Log.d(TAG, "showData: age: " + uInfo.getAge());
Log.d(TAG, "showData: handicap: " + uInfo.getHandicap());
Log.d(TAG, "showData: gender: " + uInfo.getGender());
ArrayList<String> array = new ArrayList<>();
array.add("Full Name:");
array.add(uInfo.getName());
array.add("Age:");
array.add(uInfo.getAge());
array.add("Handicap:");
array.add(uInfo.getHandicap());
array.add("Gender:");
array.add(uInfo.getGender());
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
mListView.setAdapter(adapter);
}
}
*/
private void showData(DataSnapshot dataSnapshot) {
ArrayList<String> array = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()){
UserInformation uInfo = ds.getValue(UserInformation.class);
array.add(" Full Name / " +uInfo.getName());
array.add(" Age / " + uInfo.getAge());
array.add(" Handicap/ " + uInfo.getHandicap());
array.add(" Gender/ " + uInfo.getGender());
}
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
mListView.setAdapter(adapter);
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
}
}
Currently the way each user is differentiated is by the UID which is allocated to them when they log in. When they log in they use username and password which is stored in the authentication part of Firebase. When the user has been verified they are directed to their Account Page. On the Account page I then ask the user to input their personal details which is saved in the real time database under Table Users. How do I display the current logged user information ? Currently it shows details of the user table but I only want it to show the details of the user who is logged in. The output is shown in a ListView
First get uid of the current user that is logged in:
then retrieve the data of the current user:
Assuming you have this: