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);
}
最佳答案
根据您的数据库结构,不需要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/