如何将姓名以及电子邮件和密码添加到firebase数据库android?
我添加了一个功能createUserWithEmailAndPassword
的电子邮件和密码,并调用了一个函数:
createNewUser(task.getResult().getUser());
成功时和在createnewUser中执行此操作
private void createNewUser(FirebaseUser userFromRegistration) {
String username = nameEditText.getText().toString();
String email = userFromRegistration.getEmail();
String userId = userFromRegistration.getUid();
User user = new User();
user.setName(username);
user.setUid(userId);
user.setEmail(email);
Log.d("Raza",mDatabase.child("users").push().setValue(user).isSuccessful()+"");
}
但我在Log.d
收到False并且用户名未添加到我的数据库中。
我的用户类是:
public class User {
String name;
String uid;
String email;
public User(){
// Default constructor required
}
public void setName(String name){this.name = name;}
public void setUid(String uid){this.uid = uid;}
public void setEmail(String email){this.email = email;}
public String getName(){return this.name;}
}
请帮我解决这个问题。
答案 0 :(得分:0)
public class User {
public String uid;
public String email;
public String user_name;
public User(){
}
public User(String uid, String email String user_name)
{
this.uid = uid;
this.email = email;
this.user_name=user_name;
}
}
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
User user = new User(firebaseUser.getUid(),
firebaseUser.getEmail(),
MySharedPreferences.getString(Constants.KEY_USER_NAME));
database.child(Constants.ARG_USERS)
.child(firebaseUser.getUid())
.setValue(user)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// mOnUserDatabaseListener.onSuccess(context.getString(R.string.user_successfully_added));
} else {
// mOnUserDatabaseListener.onFailure(context.getString(R.string.user_unable_to_add));
}
}
});