我的应用正在根据以下代码发送用户名
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
currentUserUid=mAuth.getCurrentUser().getUid();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("MyUserList");
myRef.child(usernameInput).child(currentUserUid).child("username").setValue(usernameInput);
sendUserToLoginActivity();
Toast.makeText(RegisterActivity.this, "Account Created Successfully",
Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
现在,我必须在另一个活动中从Firebase数据库获取用户名。我尝试了以下方法,但是每次“分数”都传递给数据库而不是用户名。我不想更改用户名设置方式(以上代码),因为我正在检查用户注册时重复的用户名。如何获取当前用户名?
private String currentUserUid;
private FirebaseAuth mAuth;
private String name;
public void finisHere(){
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Score");
String key=myRef.push().getKey();
//......................
currentUserUid= mAuth.getCurrentUser().getUid();
DatabaseReference cUserNameRef = database.getReference("MyUserList");
cUserNameRef.child(currentUserUid).addListenerForSingleValueEvent(new
ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot:snapshot.getChildren()){
// String name="";
name=dataSnapshot.child("username").getValue(String.class);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
savingScoreModel scoreModel=new savingScoreModel(score,name);
myRef.child(key).setValue(scoreModel);
Intent intent =new
Intent(liveQuestionActivity.this,MainActivity.class);
Toast.makeText(this, currentUserUid, Toast.LENGTH_SHORT).show(); //Toast is working
startActivity(intent);
finishAffinity();
}
我正在通过以下代码验证重复的用户名。
private void DuplicateUserName(){
final DatabaseReference rootRef =
FirebaseDatabase.getInstance().getReference();
DatabaseReference userNameRef =
rootRef.child("MyUserList").child(usernameInput);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()) {
//create new user
//Toast.makeText(RegisterActivity.this, "success",
Toast.LENGTH_SHORT).show();
/* FirebaseDatabase database =
FirebaseDatabase.getInstance();
DatabaseReference myRef =
database.getReference("MyUserList");
myRef.child(usernameInput).child("username").setValue(usernameInput);
*/
CreateNewAccount();
}
else {
UserName.setError("This username already taken");
UserName.requestFocus();
loadingBar.dismiss();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(RegisterActivity.this,
databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
};
userNameRef.addListenerForSingleValueEvent(eventListener);
}
答案 0 :(得分:0)
用户名已上传到"MyUserList"/usernameInput/currentUserUid/"username"
但是您正在获取"MyUserList"/currentUserUid/"username"
因此,请尝试以下操作进行上传:
myRef.child("MyUserList").child(currentUserUid).child("username").setValue(usernameInput);
希望这会有所帮助。随时要求澄清...
编辑:@Farhad怀疑后编辑
DatabaseReference reference = database.getReference("MyUserList");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
//TODO: Here are your UserNames
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});