Kitsune Hifu Kitsune Hifu - 1 month ago 7
Android Question

Why it didn't get current EditText value

I'm doing basic demo of android related to login, update user

I used user's id from login page to load user's information to update page

username = (EditText) findViewById(R.id.txtUsername);
age = (EditText) findViewById(R.id.txtAge);
weight = (EditText) findViewById(R.id.txtWeight);
height = (EditText) findViewById(R.id.txtHeight);
username.setText(db_username);
age.setText(db_age);
weight.setText(db_weight);
height.setText(db_height);


and update button

update.setOnClickListener(new View.OnClickListener() {
int sweight = Integer.parseInt(weight.getText().toString());
double sheight = Double.parseDouble(height.getText().toString());
int sage = Integer.parseInt(age.getText().toString());
@Override
public void onClick(View v) {
if (weight.equals("")) {
Toast.makeText(getApplicationContext(), "Please input weight!",
Toast.LENGTH_LONG).show();
return;
}else if(height.equals("")){
Toast.makeText(getApplicationContext(), "Please input height!",
Toast.LENGTH_LONG).show();
return;
}else if(age.equals("")){
Toast.makeText(getApplicationContext(), "Please input age!",
Toast.LENGTH_LONG).show();
return;
}else{
dbHandler.updateUser(sid,sweight,sheight,sage);
Toast.makeText(getApplicationContext(),
"User's information updated! ", Toast.LENGTH_LONG)
.show();
Toast.makeText(getApplicationContext(),
sid+sweight+sheight+sage+" and test", Toast.LENGTH_LONG)
.show();
Intent i = new Intent(UserUpdate.this, MainActivity.class);
startActivity(i);
finish();

}
}
});


For exam: weight: 70,height:1,65,age:21

when i changed it to: weight:50,height:1,65,age:21

the 2nd Toast show me that weight : 70 but not 50

Answer

You need to move these top three lines inside of the click method, not declare them as member variables of the anonymous class.

    int sweight = Integer.parseInt(weight.getText().toString());
    double sheight = Double.parseDouble(height.getText().toString());
    int sage = Integer.parseInt(age.getText().toString());

    @Override
    public void onClick(View v) {

        // Move them here
Comments