2016-10-06 26 views
0

我做的登录相关的Android的基本演示,
我用用户的ID从登录页面加载用户的信息来更新页面更新用户为什么它没有获得当前的EditText值

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

和更新按钮

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

      } 
     } 
    }); 

对于考试:重量:70,高度:1,65,年龄:21
当我它更改为:重:50,高度:1,65,年龄:21
的第二吐司给我看看wei GHT:70而不是50

+0

您可能还应该在EditTexts上使用'setError'而不是烘烤 –

回答

2

您需要将这些顶级三行在click方法内部,不要将它们声明为匿名类的成员变量。

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 
0

把这个:

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

中的onclick方法,前 - >如果(weight.equals( “”))

相关问题