gpt4 book ai didi

java - 如何显示当前登录的用户 Firebase

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:52 26 4
gpt4 key购买 nike

    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);
}
}
}
}

目前区分每个用户的方式是在他们登录时分配给他们的 UID。当他们登录时,他们使用存储在 Firebase 的身份验证部分中的用户名和密码。用户通过验证后,他们将被定向到他们的帐户页面。然后在帐户页面上,我要求用户输入他们的个人详细信息,这些详细信息保存在表用户下的实时数据库中。如何显示当前登录的用户信息?目前它显示用户表的详细信息,但我只希望它显示登录用户的详细信息。输出显示在 ListView 中

最佳答案

首先获取当前登录用户的uid:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();

然后获取当前用户的数据:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
name1.setText(name);
}

假设你有这个:

Users
userid
name: peter
//etc

关于java - 如何显示当前登录的用户 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49463940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com