当他们在我的应用程序中注册时,如何为每个用户添加主键?这是我当前使用用户名作为主键的代码(编程中的不良做法):
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog mDialog = new ProgressDialog(Register.this);
mDialog.setMessage("Please wait...");
mDialog.show();
table_user.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Check if the username already exists
if(dataSnapshot.child(username.getText().toString()).exists()){
mDialog.dismiss();
Toast.makeText(Register.this, "Username already exists", Toast.LENGTH_SHORT).show();
}
else{
mDialog.dismiss();
User user = new User(user_fname.getText().toString(), user_lname.getText().toString(), user_email.getText().toString(), user_pass.getText().toString());
table_user.child(username.getText().toString()).setValue(user);
Toast.makeText(Register.this, "Account successfully registered", Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
我的用户的模型:
public class User {
private String FirstName;
private String LastName;
private String Email;
private String Password;
public User() {
}
public User(String firstName, String lastName, String email, String password) {
FirstName = firstName;
LastName = lastName;
Email = email;
Password = password;
}
我只是 firebase 的新手,这就是为什么我很难理解它。任何帮助将不胜感激,谢谢。
Helenr
相关分类