2016-01-19 48 views
-4

我正在使用android。我正在創建一個移動應用程序,需要根據體重指數,年齡和體力活動給出1型糖尿病卡路里攝入量的建議。我的問題是我的if else語句無法正常工作。如果其他編碼在Review.java,我需要在Result.java上顯示輸出。Android;如果其他語句不起作用

Bmi.java

final Context context = this; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bmi); 

    // Find UI elements by ID and save to variable 
    final EditText height = (EditText) findViewById(R.id.height_input); 
    final EditText weight = (EditText) findViewById(R.id.weight_input); 
    final TextView bmi_result = (TextView) findViewById(R.id.bmi_result); 
    Button button = (Button) findViewById(R.id.bmi_calc_button); 
    Button next = (Button) findViewById(R.id.NavigateButton); 

    // Listen for our click event 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      //String bmi = bmi_result.getText().toString(); 

      // Check for null 
      if (height.getText().toString().length() < 1) { 
       sendToast("You must enter your height!"); 
       // Abort the onClick if null 
       return; 
      } 
      if (weight.getText().toString().length() < 1) { 
       sendToast("You must enter your weight, sorry!"); 
       // Abort the onClick if null 
       return; 
      } 
      // Passed the null checks, let's do some math! 

      /*** 
      * Android is funny this way, but you have 
      * to convert it back and forth from integer/float 
      * to strings, you'll get used to it. ;) 
      */ 
      // Convert height from string to float 
      float h = Float.valueOf(height.getText().toString()); 
      float w = Float.valueOf(weight.getText().toString()); 

      /*** 
      * Time for math! 
      * BMI is calculated 
      * (weigth in kg/(height in meter * height in meter) 
      * But since we want the user to input in CM, we just 
      * multiply it with 10 000 to get the correct value. 
      */ 
      float BMI = w/(h * h) * 10000; 

      // Set the bmi_result view item of the value of our BMI 
      bmi_result.setText(String.valueOf(BMI)); 

      String textBMI = bmi_result.getText().toString(); 
     } 
    }); 

    next.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v){ 
      String textBMI = bmi_result.getText().toString(); 
      Intent intent = new Intent(Bmi.this, Age.class); 
      intent.putExtra("Ans_bmi",textBMI); 
      startActivity(intent); 
     } 

    }); 
} 

Age.java

private RadioGroup age; 
//private RadioButton radioAgeButton; 
private Button b; 
public String selectedType= ""; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.age); 

    age = (RadioGroup) findViewById(R.id.radioGroup); 
    final RadioButton rb1 = (RadioButton) findViewById(R.id.rb1); 
    final RadioButton rb2 = (RadioButton) findViewById(R.id.rb2); 
    b = (Button) findViewById(R.id.button); 

    age.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if (checkedId == R.id.rb1) { 
       selectedType = rb1.getText().toString(); 
       Toast.makeText(Age.this, "Your age is around 41 - 45", Toast.LENGTH_LONG).show(); 
      } 
       else if (checkedId == R.id.rb2) { 
       selectedType = rb2.getText().toString(); 
       Toast.makeText(Age.this, "Your age is around 46 - 50", Toast.LENGTH_LONG).show(); 
      }//int selectedId = age.getCheckedRadioButtonId(); 
      //radioAgeButton = (RadioButton) findViewById(selectedId); 
      //Toast.makeText(Age.this, radioAgeButton.getText(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    Intent intent = getIntent(); 
    final String textBMI = intent.getStringExtra("Ans_bmi"); 

    b.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      Intent intent = new Intent(Age.this, Physical_Activity.class); 
      Bundle bundle = new Bundle(); 

      bundle.putString("Ans_bmi", textBMI); 
      bundle.putString("Ans_age", selectedType); 
      intent.putExtras(bundle); 
      startActivity(intent); 
     } 
    }); 
} 

Physical_Activity.java

private RadioGroup activity; 
//private RadioButton radioActivityButton; 
private String selectedType = ""; 
private Button b; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.physical_activity); 

    Intent intent = getIntent(); 
    final String textBMI = intent.getStringExtra("Ans_bmi"); 
    final String umur = intent.getStringExtra("Ans_age"); 

    final TextView cf_result = (TextView) findViewById(R.id.show_result); 

    activity = (RadioGroup) findViewById(R.id.radioGroup); 
    final RadioButton rbs = (RadioButton) findViewById(R.id.rbsedentary); 
    final RadioButton rbm = (RadioButton) findViewById(R.id.rbmoderate); 
    final RadioButton rba = (RadioButton) findViewById(R.id.rbactive); 
    b = (Button) findViewById(R.id.button); 

    activity.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if (checkedId == R.id.rbsedentary) { 
       selectedType = rbs.getText().toString(); 
       Toast.makeText(Physical_Activity.this, "Your physical activity is Sedentary Active", Toast.LENGTH_LONG).show(); 
      } else if (checkedId == R.id.rbmoderate) { 
       selectedType = rbm.getText().toString(); 
       Toast.makeText(Physical_Activity.this, "Your physical activity is Moderately Active", Toast.LENGTH_LONG).show(); 
      } else if (checkedId == R.id.rbactive) { 
       selectedType = rba.getText().toString(); 
       Toast.makeText(Physical_Activity.this, "Your physical activity is Active", Toast.LENGTH_LONG).show(); 
      } 
      //int selectedId = activity.getCheckedRadioButtonId(); 
      //radioActivityButton = (RadioButton)findViewById(selectedId); 
      //Toast.makeText(Physical_Activity.this, radioActivityButton.getText(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(Physical_Activity.this, Review.class); 
      Bundle bundle = new Bundle(); 

      bundle.putString("Ans_bmi", textBMI); 
      bundle.putString("Ans_age", umur); 
      bundle.putString("Ans_phy", selectedType); 
      intent.putExtras(bundle); 
      startActivity(intent); 
     } 
    }); 
} 

Review.java

protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.review); 

    //Create a bundle object to store the bundle added to the intent 
    final Bundle bundle = getIntent().getExtras(); 

    //Get the values out by key 
    final String textBMI = bundle.getString("Ans_bmi"); 
    final String umur = bundle.getString("Ans_age"); 
    final String aktiviti = bundle.getString("Ans_phy"); 

    final String txtcf = bundle.getString("cfDisease"); 

    //Get the textview controls 
    final TextView txtage = (TextView) findViewById(R.id.textView5); 
    final TextView txtphy = (TextView) findViewById(R.id.textView6); 
    final TextView txtbmi = (TextView) findViewById(R.id.textView7); 

    final TextView cf_result = (TextView) findViewById(R.id.result); 

    //Button buttonSend = (Button) findViewById(R.id.btnsend); 

    //Set the text values of the text controls 
    txtage.setText(textBMI); 
    txtphy.setText(umur); 
    txtbmi.setText(aktiviti); 
    cf_result.setText(txtcf); 

    Button submit = (Button) findViewById(R.id.btnsend); 


    final Intent intentsubmit = new Intent(); 

    intentsubmit.setClass(Review.this, Result.class); 
    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(intentsubmit); 

      String calories; 

      if (txtage.toString().equals(context.getString(R.string.age1))) 
      { 
       if (txtphy.toString().equals(context.getString(R.string.moderate))) 
       { 
        calories = "1800 cal"; 

        cf_result.setText(String.valueOf(calories)); 
        String txtcf = cf_result.getText().toString(); 
        Intent intent = new Intent(Review.this, Result.class); 
        intentsubmit.setClass(Review.this, Result.class); 
        intentsubmit.putExtra("cfDisease", txtcf); 
        startActivity(intentsubmit); 
       } 
      } 
      else if (txtage.toString().equals(context.getString(R.string.age2))) 
      { 
       if (txtphy.toString().equals(context.getString(R.string.active))) 
       { 
        calories = "2000 cal"; 

        cf_result.setText(String.valueOf(calories)); 

        String txtcf = cf_result.getText().toString(); 
        Intent intent = new Intent(Review.this, Result.class); 
        intentsubmit.setClass(Review.this, Result.class); 
        intentsubmit.putExtra("cfDisease", txtcf); 
        startActivity(intentsubmit); 
       } 
      } 
     } 
    }); 
} 

Result.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.result); 

    Intent intent = getIntent(); 
    final String txtcf = intent.getStringExtra("cfDisease"); 

    final TextView keputusan = (TextView) findViewById(R.id.show_result); 

    keputusan.setText(txtcf); 
} 
+0

'我的問題是我的輸出沒有working.'你這是什麼意思呢? – njzk2

+0

我的輸出將是「卡路里」。但輸出沒有出現 –

+0

你得到了什麼輸出?請解釋發生了什麼。 – APC

回答

0

在你的代碼,你檢查bundle.getString("Ans_bmi") == R.string.age1bundle.getString("Ans_age") == R.string.moderate

- 或 -

bundle.getString("Ans_bmi") == R.string.age2bundle.getString("Ans_age") == R.string.active

如果沒有這些條件都是真的,你的代碼不會成功。我認爲你的錯誤是在Review.java:

//Get the values out by key 
final String textBMI = bundle.getString("Ans_bmi"); 
final String umur = bundle.getString("Ans_age"); 
final String aktiviti = bundle.getString("Ans_phy"); 

//Set the text values of the text controls 
txtage.setText(textBMI); 
txtphy.setText(umur); 
txtbmi.setText(aktiviti); 

我的解決方案

要看你正在把字符串。我重新安排的變量,改變了名稱,以使更多的意義:

//Get the values out by key 
final String textBMI = bundle.getString("Ans_bmi"); 
final String textAge = bundle.getString("Ans_age"); 
final String textPhy = bundle.getString("Ans_phy"); 

//Set the text values of the text controls 
txtage.setText(textAge); 
txtphy.setText(textPhy); 
txtbmi.setText(textBMI); 
+0

非常感謝!它的工作原理:) @mmarkman –