Java 当我按下从数据库检索数据的更新按钮时,如何立即更改TextView的值?
//此变量转到(TextInputLayout“fullNameInput”)和(TextView fullName)。此变量用于更新用户的全名,也是摩托车品牌。它应同时更新TextView(全名和摩托车)同时从数据库中检索数据,只要我单击表单下方的更新按钮,它就会立即更新。但它仅在新登录后更新。(数据库的表名为users,第一个子项为用户名)Java 当我按下从数据库检索数据的更新按钮时,如何立即更改TextView的值?,java,android,android-studio,textview,android-textinputlayout,Java,Android,Android Studio,Textview,Android Textinputlayout,//此变量转到(TextInputLayout“fullNameInput”)和(TextView fullName)。此变量用于更新用户的全名,也是摩托车品牌。它应同时更新TextView(全名和摩托车)同时从数据库中检索数据,只要我单击表单下方的更新按钮,它就会立即更新。但它仅在新登录后更新。(数据库的表名为users,第一个子项为用户名) 你想尝试什么是不可理解的你不明白@ashis你的onclicklistener不工作吗?不,我的onclicklistener工作正常,因为它更新了数
你想尝试什么是不可理解的你不明白@ashis你的onclicklistener不工作吗?不,我的onclicklistener工作正常,因为它更新了数据库上的数据。并将更新的数据保存在TextInputLayouts上。唯一的问题是它不更新我的两个TextView。你能看到附件中的图像吗?然后你可以nt是否也要在单击时更改数据?
public class UserprofileActivity extends AppCompatActivity {
// VARIABLES
TextView fullName, userName, location, motorcycle;
TextInputLayout fullNameInput, emailInput, motorcycleInput, passwordInput;
// GLOBAL VARIABLES TO HOLD USER DATA INSIDE THIS ACTIVITY
String _NAME, _USERNAME, _EMAIL, _LOCATION, _MOTORCYCLE, _PASSWORD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userprofile);
reference = FirebaseDatabase.getInstance().getReference("users");
// HOOKS
fullName = findViewById(R.id.profileFullName);
userName = findViewById(R.id.profileUsername);
location = findViewById(R.id.profileLocalization);
motorcycle = findViewById(R.id.profileMotorcycle);
fullNameInput = findViewById(R.id.profile_FullName);
emailInput = findViewById(R.id.profile_Email);
motorcycleInput = findViewById(R.id.profile_Motorcycle);
passwordInput = findViewById(R.id.profile_Password);
// SHOW ALL DATA
showAllUserData();
}
private void showAllUserData() {
Intent intent = getIntent();
_NAME = intent.getStringExtra("name");
_USERNAME = intent.getStringExtra("username");
_EMAIL = intent.getStringExtra("email");
_LOCATION = intent.getStringExtra("location");
_MOTORCYCLE = intent.getStringExtra("motorcycle");
_PASSWORD = intent.getStringExtra("password");
fullName.setText(_NAME);
userName.setText(_USERNAME);
location.setText(_LOCATION);
motorcycle.setText(_MOTORCYCLE);
Objects.requireNonNull(fullNameInput.getEditText()).setText(_NAME);
Objects.requireNonNull(emailInput.getEditText()).setText(_EMAIL);
Objects.requireNonNull(motorcycleInput.getEditText()).setText(_MOTORCYCLE);
Objects.requireNonNull(passwordInput.getEditText()).setText(_PASSWORD);
}
private boolean isNameChanged() {
if (!_NAME.equals(Objects.requireNonNull(fullNameInput.getEditText()).getText().toString())) {
reference.child(_USERNAME).child("name").setValue(fullNameInput.getEditText().getText().toString());
fullName.getText().equals(_NAME);
return true;
} else {
return false;
}
}
private boolean isMotorcycleChanged() {
if (!_MOTORCYCLE.equals(Objects.requireNonNull(motorcycleInput.getEditText()).getText().toString())) {
reference.child(_USERNAME).child("motorcycle").setValue(motorcycleInput.getEditText().getText().toString());
return true;
} else {
return false;
}
}
public void update(View view) {
if (isNameChanged() || isEmailChanged() || isMotorcycleChanged() || isPasswordChanged()) {
Toast.makeText(this, "Data has been Updated Successfully", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Data is the Same and therefore Cannot be Updated", Toast.LENGTH_LONG).show();
}
}
}