我在做一个编辑页面中火力地堡的用户配置文件。我已经尝试了几种不同的方式。但我不能更新。只需添加一个新用户到数据库。
我在警告对话框中获得新的价值。请帮我。
我的更新方法的代码是:
public void editAlert() {
LayoutInflater layoutInflater = LayoutInflater.from(ProfilePage.this);
View design = layoutInflater.inflate(R.layout.edit_profile, null);
final EditText editTextUserName = design.findViewById(R.id.username_editTextProfileEdit);
final EditText editTextRealName = design.findViewById(R.id.realName_editTextProfileEdit);
final EditText editTextSurname = design.findViewById(R.id.username_editTextProfileEdit);
final EditText editTextEmail = design.findViewById(R.id.email_editTextProfileEdit);
final EditText editTextPassword = design.findViewById(R.id.password_editTextProfileEdit);
AlertDialog.Builder alertDialoga = new AlertDialog.Builder(ProfilePage.this);
alertDialoga.setTitle("Edit Profile");
alertDialoga.setView(design);
alertDialoga.setPositiveButton("Finish", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String username = editTextUserName.getText().toString().trim();
String realName = editTextRealName.getText().toString().trim();
String surname = editTextSurname.getText().toString().trim();
String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
String admin = "false";
String url = "test_url";
String key = myRef.push().getKey();
Users user = new Users(key,username,realName,surname,email,password,url,admin);
HashMap<String,Object> data = new HashMap<>();
data.put("user_email", email);
data.put("user_name", realName);
data.put("user_password", password);
data.put("user_surname", surname);
data.put("username", username);
myRef.child(user.getUser_id()).updateChildren(data);
Toast.makeText(ProfilePage.this, "Uptaded!", Toast.LENGTH_SHORT).show();
}
});
alertDialoga.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alertDialoga.show();
}
我创建用户代码的:
// Sign Up Method
// Kullanıcı Kayıt etme metodu
public void signUp(View view) {
UUID uuid = UUID.randomUUID();
final String imageName = "ProfileImages/"+uuid+".jpg";
final ProgressDialog dialog = new ProgressDialog(signupPage.this);
dialog.setTitle("Creating user record.. ");
dialog.setMessage("User registration is in progress..");
dialog.show();
StorageReference storageReference = mStorageRef.child(imageName);
storageReference.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Url
StorageReference newReference = FirebaseStorage.getInstance().getReference(imageName);
newReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
dowloadURL = uri.toString();
if (dowloadURL != null) {
mAuth.createUserWithEmailAndPassword(emailText.getText().toString(), passwordText.getText().toString())
.addOnCompleteListener(signupPage.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) /* Kullanıcı girişi başarılı ise bu çalışacak */ {
Toast.makeText(signupPage.this, "User Created", Toast.LENGTH_SHORT).show();
String userName = user_name.getText().toString();
String userSurname = user_surname.getText().toString();
String username = user_username.getText().toString();
String user_email = emailText.getText().toString();
String key = myRef.push().getKey();
String password = user_password.getText().toString();
String imageURL = dowloadURL;
Users user = new Users(key, userName, username, userSurname, user_email, password,imageURL, admin);
myRef.push().setValue(user);
Intent homePage = new Intent(signupPage.this, ProfilePage.class);
startActivity(homePage);
finish();
dialog.dismiss();
} else /* Kullanıcı girişi başarısız ise bu çalışacak */ {
/*Intent signBack = new Intent(signupPage.this, signupPage.class);
startActivity(signBack);
finish(); */
dialog.dismiss();
}
}
}).addOnFailureListener(signupPage.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(signupPage.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(signupPage.this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
下载网址来自一个单独的图像选择方法,顺便说一句。我的用户创建的代码都是这样的。
你的问题是,而不是存储在你的火力数据库恒定和有效的关键,每次你改变你的个人资料,你创建一个新的节点。为何如此?好了,你这样做:
String key = myRef.push().getKey();
其中每一次创建一个新的节点(这就是为什么推的是那里),你会得到该节点的密钥。这也是为什么你创建一个新用户,而不是更新您的帐户资料。做正确的方法如下。
当创建用户获得与此键:
String key = FirebaseAuth.getInstance().getCurrentUser().getUid();
在创建这个按键用户对象,做到以下几点:
myRef.child(key).setValue(user);
当你想更新你的用户,你可以访问密钥创建同样的方式。让所有的更新信息和密钥后,然后执行:
myRef.child(key).setValue(data); //For updating
要么
myRef.child(key).updateChildren(data); //For updating
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句