Java 使用firebase当前uid而不是随机uid保存我的文档文件
所以,当我的用户在firebase数据库中注册自己时,他的ID是随机生成的,我想将新注册用户的UID作为firebase中文档的ID,我如何更改它?我还没有找到任何使文档另存为字符串或类似内容的函数 这是我的注册码:Java 使用firebase当前uid而不是随机uid保存我的文档文件,java,android,firebase,google-cloud-firestore,firebase-authentication,Java,Android,Firebase,Google Cloud Firestore,Firebase Authentication,所以,当我的用户在firebase数据库中注册自己时,他的ID是随机生成的,我想将新注册用户的UID作为firebase中文档的ID,我如何更改它?我还没有找到任何使文档另存为字符串或类似内容的函数 这是我的注册码: import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.ut
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
public class RegisterActivity extends AppCompatActivity {
private EditText UserName, FullName, eMail1, eMail2, Password, Day, Month, Year;
private Button registerButton;
private CheckBox male, female, other;
private FirebaseAuth mAuth;
private ProgressDialog loadingBar;
public String currentUserId, username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
loadingBar = new ProgressDialog(this);
UserName = (EditText) findViewById(R.id.register_username);
FullName = (EditText) findViewById(R.id.register_name);
eMail1 = (EditText) findViewById(R.id.register_email);
eMail2 = (EditText) findViewById(R.id.register_email_2);
Password = (EditText) findViewById(R.id.register_password);
Day = (EditText) findViewById(R.id.register_birthday_day);
Month = (EditText) findViewById(R.id.register_birthday_month);
Year = (EditText) findViewById(R.id.register_birthday_year);
registerButton = (Button) findViewById(R.id.register_button);
male = (CheckBox) findViewById(R.id.register_male);
other = (CheckBox) findViewById(R.id.register_other);
female = (CheckBox) findViewById(R.id.register_female);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CreateNewAccount();
}
});
}
private void CreateNewAccount() {
username = UserName.getText().toString();
String fullname = FullName.getText().toString();
String email1 = eMail1.getText().toString();
String email2 = eMail2.getText().toString();
String password = Password.getText().toString();
String day = Day.getText().toString();
String month = Month.getText().toString();
String year = Year.getText().toString();
if(TextUtils.isEmpty(username)){
Toast.makeText(this,"Please type in your username...", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(fullname)){
Toast.makeText(this,"Please type in your full name...", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(email1)){
Toast.makeText(this,"Please type in your eMail...", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(email2)){
Toast.makeText(this,"Please retype your eMail again...", Toast.LENGTH_SHORT).show();
}
else if(!email1.equals(email2)){
Toast.makeText(this,"You entered two different emails...? Please try again", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(password)){
Toast.makeText(this,"Please type in your password...", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(day)){
Toast.makeText(this,"Please enter your birthday day...", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(month)){
Toast.makeText(this,"Please enter your birthday month...", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(year)){
Toast.makeText(this,"Please enter your birthday year...", Toast.LENGTH_SHORT).show();
}
else if(!male.isChecked() && !female.isChecked() && !other.isChecked()){
Toast.makeText(this,"Chose your gender...", Toast.LENGTH_SHORT).show();
}
else if(male.isChecked() && female.isChecked()){
Toast.makeText(this,"Please choose only one gender...", Toast.LENGTH_SHORT).show();
}
else if(male.isChecked() && other.isChecked()){
Toast.makeText(this,"Please choose only one gender...", Toast.LENGTH_SHORT).show();
}
else if(female.isChecked() && other.isChecked()){
Toast.makeText(this,"Please choose only one gender...", Toast.LENGTH_SHORT).show();
}
else{
loadingBar.setTitle("Creating New Account");
loadingBar.setMessage("Please wait, while we are creating your new Account...");
loadingBar.show();
loadingBar.setCanceledOnTouchOutside(true);
mAuth.createUserWithEmailAndPassword(email1, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
FirebaseUser user = mAuth.getCurrentUser();
SaveAccountInformation();
Toast.makeText(RegisterActivity.this,"Your account was created please check your email for an activation mail", Toast.LENGTH_LONG).show();
loadingBar.dismiss();
SendUserToMainActivity();
}
else{
String message= task.getException().getMessage();
Toast.makeText(RegisterActivity.this, "Error Occured: " + message, Toast.LENGTH_LONG).show();
loadingBar.dismiss();
}
}
});
}
}
private void SaveAccountInformation() {
currentUserId = mAuth.getCurrentUser().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();
String gender;
username = UserName.getText().toString();
String fullname = FullName.getText().toString();
String day = Day.getText().toString();
String month = Month.getText().toString();
String year = Year.getText().toString();
if(male.isChecked()) {
gender = "male";
} else if(female.isChecked()){
gender = "female";
} else {
gender = "other";
}
HashMap userMap = new HashMap();
userMap.put("username", username);
userMap.put("gender", gender);
userMap.put("fullname", fullname);
userMap.put("birthday", day+"/"+month+"/"+year);
userMap.put("uid", mAuth.getCurrentUser().getUid());
userMap.put("verified", "false");
db.collection("users")
.add(userMap)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
private void SendUserToMainActivity() {
Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
}
}
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.Bundle;
导入android.text.TextUtils;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.CheckBox;
导入android.widget.EditText;
导入android.widget.Toast;
导入androidx.annotation.NonNull;
导入androidx.appcompat.app.appcompat活动;
导入com.google.android.gms.tasks.OnCompleteListener;
导入com.google.android.gms.tasks.OnFailureListener;
导入com.google.android.gms.tasks.OnSuccessListener;
导入com.google.android.gms.tasks.Task;
导入com.google.firebase.auth.AuthResult;
导入com.google.firebase.auth.FirebaseAuth;
导入com.google.firebase.auth.FirebaseUser;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.firebase.firestore.DocumentReference;
导入com.google.firebase.firestore.FirebaseFirestore;
导入java.util.HashMap;
公共类注册活动扩展了AppCompatActivity{
私人编辑文本用户名、全名、eMail1、eMail2、密码、日期、月份、年份;
专用按钮寄存器按钮;
私人复选框男性、女性、其他;
私人消防队;
私有进程对话框加载栏;
公共字符串currentUserId,用户名;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u寄存器);
mAuth=FirebaseAuth.getInstance();
loadingBar=新建进度对话框(此对话框);
用户名=(EditText)findViewById(R.id.register\u用户名);
全名=(EditText)findViewById(R.id.register\u name);
eMail1=(EditText)findViewById(R.id.register\u电子邮件);
eMail2=(EditText)findViewById(R.id.register\u email\u 2);
密码=(EditText)findViewById(R.id.register\u密码);
Day=(EditText)findViewById(R.id.register\u Birth\u Day);
月份=(EditText)findViewById(R.id.register\u Birth\u Month);
年份=(EditText)findViewById(R.id.register\u Birth\u Year);
registerButton=(按钮)findViewById(R.id.register_按钮);
男性=(复选框)findViewById(R.id.register\u男性);
其他=(复选框)findviewbyd(R.id.register\u other);
女性=(复选框)findviewbyd(R.id.register\u女性);
registerButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
CreateNewAccount();
}
});
}
私有void CreateNewAccount(){
username=username.getText().toString();
字符串fullname=fullname.getText().toString();
字符串email1=email1.getText().toString();
字符串email2=email2.getText().toString();
字符串密码=password.getText().toString();
字符串day=day.getText().toString();
字符串month=month.getText().toString();
字符串year=year.getText().toString();
if(TextUtils.isEmpty(用户名)){
Toast.makeText(这是“请键入您的用户名…”,Toast.LENGTH_SHORT.show();
}
else if(TextUtils.isEmpty(全名)){
Toast.makeText(这是“请键入您的全名…”),Toast.LENGTH_SHORT.show();
}
else if(TextUtils.isEmpty(email1)){
Toast.makeText(这是“请键入您的电子邮件…”),Toast.LENGTH_SHORT.show();
}
else if(TextUtils.isEmpty(email2)){
Toast.makeText(这是“请再次键入您的电子邮件…”),Toast.LENGTH_SHORT.show();
}
如果(!email1.equals(email2)){
Toast.makeText(这是“您输入了两封不同的电子邮件…?请再试一次”,Toast.LENGTH_SHORT.show();
}
else if(TextUtils.isEmpty(密码)){
Toast.makeText(这是“请输入密码…”),Toast.LENGTH_SHORT.show();
}
else if(TextUtils.isEmpty(天)){
Toast.makeText(这是“请输入您的生日…”),Toast.LENGTH_SHORT.show();
}
else if(TextUtils.isEmpty(月)){
Toast.makeText(这是“请输入您的生日月份…”),Toast.LENGTH_SHORT.show();
}
else if(TextUtils.isEmpty(年)){
Toast.makeText(这是“请输入您的生日年…”),Toast.LENGTH_SHORT.show();
}
else如果(!male.isChecked()&&&!female.isChecked()&&!other.isChecked()){
Toast.makeText(这是“选择你的性别…”),Toast.LENGTH_SHORT.show();
}
else if(male.isChecked()&&male.isChecked()){
Toast.makeText(这是“请只选择一种性别…”),Toast.LENGTH_SHORT.show();
}
else if(male.isChecked()&&other.isChecked()){
Toast.makeText(这是“请只选择一种性别…”),Toast.LENGTH_SHORT.show();
}
else if(female.isChecked()&&other.isChecked()){
Toast.makeText(这是“请只选择一种性别…”),Toast.LENGTH_SHORT.show();
}
否则{
loadingBar.setTitle(“创建新帐户”);
loadingBar.setMessage(“请稍候,我们正在创建您的新帐户…”);
loadingBar.show();
loadingBar.SetCanceledOnTouchOut(真);
mAuth.createUserWithEmailAndPassword(email1,password).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
火基用途
db.collection("users").document(currentUserId)
.set(userMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//Do what you want
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});