gpt4 book ai didi

java - 陷入通过 Firebase 获取名称的困境

转载 作者:行者123 更新时间:2023-12-01 17:56:32 25 4
gpt4 key购买 nike

public
class AccountActivity extends AppCompatActivity {
private
TextView showprofile;
private TextView showemail;
private
DatabaseReference userprofilereference;
private FirebaseAuth mAuth;
// private String CurrentUserId;
private
FirebaseUser user;
private Button delete;

@Override
protected
void onCreate ( Bundle savedInstanceState ) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_account);

mAuth = FirebaseAuth.getInstance ();
user = mAuth.getCurrentUser ();
userprofilereference = FirebaseDatabase.getInstance ().getReference ().child (user.getUid ());
showemail = (TextView) findViewById (R.id.edit_emailp);
showemail.setText (user.getEmail ());
showprofile = (TextView) findViewById (R.id.profile_name_textView);
delete = (Button) findViewById (R.id.deleteaccount);


delete.setOnClickListener (new View.OnClickListener () {
@Override
public
void onClick ( View view ) {
deleteUser ();
startActivity (new Intent (AccountActivity.this, SignUpActivity.class));

}
});
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.child ("name");
String name = dataSnapshot.getValue (String.class);
showprofile.setText (name);
}

@Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);

}

enter image description here

最佳答案

根据您的数据库结构,不需要 uid。

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
showprofile.setText (name);
}

@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // never ignore errors
}
};
uidRef.addListenerForSingleValueEvent(eventListener);

关于java - 陷入通过 Firebase 获取名称的困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60706392/

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