2016-10-06 44 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( 「」))

相關問題