首页 > 解决方案 > 当我按下从数据库中检索数据的更新按钮时,如何立即更改 TextView 的值?

问题描述

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");

// 这个变量去 (TextInputLayout "fullNameInput) 和 (TextView fullName) 这去更新用户的全名,也是摩托车品牌。它应该用从数据库中检索到的数据更新 TextViews(Full Name 和 Motorcycle)同时,只要我点击表单下方的更新按钮。但它只会在新登录后更新。(数据库的表名是用户,第一个孩子是用户名)。在此处输入图像描述

        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();
        }
    }
}

标签: javaandroidandroid-studiotextviewandroid-textinputlayout

解决方案


推荐阅读