我想更新密码值,我将输入用户名并搜索与用户名文本匹配的父节点以更改密码。 这是我的代码
db = FirebaseDatabase.getInstance().getReference();
logIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.setMessage("Changing Password");
newPassword();
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Change successful.", Toast.LENGTH_LONG).show();
}
});//inside of onCreate method
private void newPassword(){
if(TextUtils.isEmpty(userText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input username.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(emailText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input email.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(contactText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input contact no.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(firstText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input first name.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(lastText.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input last name.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(password.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input password.", Toast.LENGTH_LONG).show();
return;
}
if(TextUtils.isEmpty(retype.getText().toString().trim())){
progressDialog.dismiss();
Toast.makeText(this, "Input retype password.", Toast.LENGTH_LONG).show();
return;
}
db.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String username = userText.getText().toString().trim();
for(DataSnapshot ds: dataSnapshot.getChildren()){
if(username.matches(ds.getKey())){
user.setPassword(password.getText().toString().trim());
ds.getRef().child(username).child("password").setValue(user.getPassword());
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("User", databaseError.getMessage());
}
});
}
我的问题是如何到达我想要更改其值的节点,每次按下按钮时,密码的值都不会改变。
感谢您的帮助。
EDITED
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
...
progressDialog = new ProgressDialog(this);
db = FirebaseDatabase.getInstance().getReference();
db.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
username = (String)dataSnapshot.child(userText.getText().toString().trim()).child("username").getValue();
address = (String)dataSnapshot.child(userText.getText().toString().trim()).child("address").getValue();
contact = (String)dataSnapshot.child(userText.getText().toString().trim()).child("contact").getValue();
email = (String)dataSnapshot.child(userText.getText().toString().trim()).child("email").getValue();
first = (String)dataSnapshot.child(userText.getText().toString().trim()).child("first").getValue();
last = (String)dataSnapshot.child(userText.getText().toString().trim()).child("last").getValue();
pass = passwordText.getText().toString().trim();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(ForgotPassword.this, "Database Error", Toast.LENGTH_LONG).show();
Intent i = new Intent(ForgotPassword.this, SignIn.class);
startActivity(i);
}
});
logIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newPassword();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ForgotPassword.this, SignIn.class);
startActivity(i);
}
});
}
private void newPassword(){
...
user = new User(first, last, email, contact, pass, username, address);
db.child("users").child(username).setValue(user);
Toast.makeText(getApplicationContext(), "Change successful.", Toast.LENGTH_LONG).show();
Intent i = new Intent(this, TabMenu.class);
startActivity(i);
}
每次我开始此活动时,它都会立即在onCancelled(DatabaseError){Toast.makeText(..., "Database Error", ...).show();}
答案 0 :(得分:1)
如果要比较Java中的两个字符串,则需要使用equals
方法,而不是matches
。
matches方法判断此字符串是否与给定的正则表达式匹配。
调用str.matches(regex)形式的此方法会产生与表达式
完全相同的结果java.util.regex.Pattern.matches(regex,str)。
所以改变这行代码:
username.matches(ds.getKey())
与
username.equals(ds.getKey())
要更改密码,您只需要直接在引用上使用setValue()
方法,如下所示:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("users").child("brian").child("password").setValue("newPassword");