3

I'm trying to calculate the BMR for women but spinner is giving me error:

09-01 22:08:52.373: E/AndroidRuntime(344):  at com.fps.iHealthFirst.BMRCalculator.onClick(BMRCalculator.java:286)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.view.View.performClick(View.java:2485)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.view.View$PerformClick.run(View.java:9080)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.os.Handler.handleCallback(Handler.java:587)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.os.Looper.loop(Looper.java:123)
09-01 22:08:52.373: E/AndroidRuntime(344):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-01 22:08:52.373: E/AndroidRuntime(344):  at java.lang.reflect.Method.invokeNative(Native Method)
09-01 22:08:52.373: E/AndroidRuntime(344):  at java.lang.reflect.Method.invoke(Method.java:507)
09-01 22:08:52.373: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-01 22:08:52.373: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-01 22:08:52.373: E/AndroidRuntime(344):  at dalvik.system.NativeStart.main(Native Method)

and my code below, trying to get the value of spinners that are sedentary, lightly active, moderately active and heavily active:

private void calculateWomen(){

    String f1a = etft.getText().toString();
    String f2a = etin.getText().toString();
    String f3a = etweight.getText().toString();
    String f4a = etage.getText().toString();

    if ( ( f1a.isEmpty() || f2a.isEmpty() || f3a.isEmpty() || f4a.isEmpty() ) ) 
        // call for custom toast
        viewErrorToast();
    else{
    // Metric Formula for BMR (Women) English Unit
    //655 + ( 4.3379 x weight in pounds ) + 
    //( 4.6980 x height in inches ) - ( 4.6756 x age in years )
     String age, in, ft, weight;
     Double answer;

     age =  etage.getText().toString();
     in =  etin.getText().toString();    
     ft = etft.getText().toString();
     weight = etweight.getText().toString();


        if (spinnerText.equals("Sedentary")){
            //actAnswer = "1.2";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.2);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }
        else if (spinnerText.equals("Lightly Active")){
            //actAnswer = "1.375";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.375);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }
        else if (spinnerText.equals("Moderately Active")){
            // actAnswer = "1.55";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.55);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }
        else if (spinnerText.equals("Heavily Active")){
            // actAnswer = "1.725";
             answer =  ( ( 665 + ( 4.3379 * Double.parseDouble( weight ) ) + 
                     ( 4.6980 * ( Double.parseDouble( in ) ) * 12 + Double.parseDouble( ft ) )
                     - ( 4.6756 * Double.parseDouble( age ) ) )  * 1.725);
             BigDecimal bd = BigDecimal.valueOf(answer);
             bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
             etanswer.setText(bd.toString());  
        }


     // call for custom toast
     viewBMRSavedToast();
    }

} // end of calculateWomen method

If anything's wrong that you notice, please inform me. thanks. would appreciate it.

Dunkey
  • 1,900
  • 11
  • 42
  • 72
  • 1
    Is this _all_ of the logcat errors? If so your error is in your `onClick(View v)` method not `calculateWomen()`. Please post your onClick() method and indicate with line is 286: "`at com.fps.iHealthFirst.BMRCalculator.onClick(BMRCalculator.java:286) `" – Sam Sep 01 '12 at 16:03

2 Answers2

3

You may use this method:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Helal Khan
  • 867
  • 3
  • 10
  • 25
  • if ( spinnerText.equals( "Sedentary" ) ){ // "1.2" if ( male.isChecked( ) ) { answer = ( ( 66 + ( 6.2377 * Double.parseDouble( weight ) ) + ( 12.7084 * ( Double.parseDouble( in ) * 12 + Double.parseDouble( ft ) ) ) - ( 6.8 * Double.parseDouble( age ) ) ) * 1.2 ); BigDecimal bd = BigDecimal.valueOf( answer ); bd = bd.setScale( 2, BigDecimal.ROUND_FLOOR ); etanswer.setText( bd.toString() ); } – Dunkey Sep 01 '12 at 14:45
  • when the user would click the calculate button, it should display the result. the calculation shouldn't be placed in setOnItemSelectedListener.. where else can I place it? – Dunkey Sep 01 '12 at 15:03
0

If Do you want the spinner value while selection of items then you should use spinner onitemselected listener inside of coding. < Android Spinner: Get the selected item change event > this links will helps you further.

Community
  • 1
  • 1
Venkatesh
  • 144
  • 1
  • 18