简体   繁体   English

Android Spinner getSelected项目

[英]android spinner getselected item

In my Spinner I have a String Array for it {sedentary, lightly active, moderately active and heavily active} (this is the calculation of one's BMR) 在我的Spinner我有一个String Array {精打细算,轻度活跃,中度活跃和重度活跃}(这是一个人的BMR的计算结果)

What I want to do is get the selected item in a Spinner . 我想做的是在Spinner获得选定的项目

For example, when I click on sedentary , for sedentary has a value of 1.2 , the result of the bmr is multiplied by 1.2 . 例如,当我单击久坐时 ,由于久坐的值为1.2bmr的结果将multiplied 1.2 But I can't seem to get it work for me. 但是我似乎无法让它对我有用。

Please do check my code below: 请在下面检查我的代码:

private void calculateMen(){    
    String f1 = etft.getText().toString();
    String f2 = etin.getText().toString();
    String f3 = etweight.getText().toString();
    String f4 = etage.getText().toString();

    if ( (f1.isEmpty() || f2.isEmpty() || f3.isEmpty() || f4.isEmpty() ) ) 
    {   // call for custom toast
        viewErrorToast();
    } 
    else
    {
        // Metric Formula for BMR (Men) English Unit
        // 66 + ( 6.2377 x weight in kilos ) + 
        //( 12.7084 x height in cm ) - ( 6.7550 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"))
        {     
            answer =  ( ( 66 + ( 6.2377 * Double.parseDouble( weight ) ) + ( 12.7084 * ( Double.parseDouble( in ) * 12 + Double.parseDouble( ft ) ) ) - ( 6.8 * Double.parseDouble( age ) ) ) * 1.2 ); 
            //*  Double.parseDouble(actAnswer) ;    
            BigDecimal bd = BigDecimal.valueOf(answer);
            bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);         
            etanswer.setText(bd.toString()); 
        }
        else if (spinnerText.equals("Lightly Active"))
        {
            answer =  ( ( 66 + ( 6.2377 * Double.parseDouble( weight ) ) + ( 12.7084 * ( Double.parseDouble( in ) * 12 + Double.parseDouble( ft ) ) ) - ( 6.8 * Double.parseDouble( age ) ) ) * 1.375 ); 
            //*  Double.parseDouble(actAnswer) ;    
            BigDecimal bd = BigDecimal.valueOf(answer);
            bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
            etanswer.setText(bd.toString()); 
        }
        else if (spinnerText.equals("Moderately Active"))
        {
          answer =  ( ( 66 + ( 6.2377 * Double.parseDouble( weight ) ) + ( 12.7084 * ( Double.parseDouble( in ) * 12 + Double.parseDouble( ft ) ) ) - ( 6.8 * Double.parseDouble( age ) ) ) * 1.55 ); 
          //*  Double.parseDouble(actAnswer) ;    
          BigDecimal bd = BigDecimal.valueOf(answer);
          bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
          etanswer.setText(bd.toString()); 
        }
        else if (spinnerText.equals("Heavily Active"))
        {
          answer =  ( ( 66 + ( 6.2377 * Double.parseDouble( weight ) ) + ( 12.7084 * ( Double.parseDouble( in ) * 12 + Double.parseDouble( ft ) ) ) - ( 6.8 * Double.parseDouble( age ) ) ) * 1.725 ); 
          //*  Double.parseDouble(actAnswer) ;    
          BigDecimal bd = BigDecimal.valueOf(answer);
          bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
          etanswer.setText(bd.toString()); 
        }
        // call for custom toast
        viewBMRSavedToast();
    }

} // end of calculateMen method

Please try modifying the code as below : 请尝试如下修改代码:

Create a field : 创建一个字段:

private HashMap<String, Double> spinnerValues;

From onCreate of your activity, call the below function : 在活动的onCreate中,调用以下函数:

private void createSpinnerValues(){
    spinnerValues=new HashMap<String, Double>();
    spinnerValues.put("Sedentary", 1.2);
    spinnerValues.put("Lightly Active", 1.375);
    spinnerValues.put("Moderately Active", 1.55);
    spinnerValues.put("Heavily Active", 1.725);
}

Then find your modified function to calculate as below : 然后找到修改后的函数进行如下计算:

private void calculateMen(){

    //Your Code Goes here...

     String age, in, ft, weight;
     Double answer;
     age =  etage.getText().toString();
     in =  etin.getText().toString();    
     ft = etft.getText().toString();
     weight = etweight.getText().toString();

     answer=getAnswer(age,in,ft,weight,getSelectedItemValueFromSpinnerText(spinnerText));

     BigDecimal bd = BigDecimal.valueOf(answer);
     bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);

     etanswer.setText(bd.toString()); 

     // call for custom toast
     viewBMRSavedToast();
    }

private Double getAnswer(String age, String in, String ft, String weight,
        Double selectedItemValueFromSpinner) {
    double answer = ( ( 66 + ( 6.2377 * Double.parseDouble( weight ) ) + 
             ( 12.7084 * ( Double.parseDouble( in ) * 12 + Double.parseDouble( ft ) ) )
             - ( 6.8 * Double.parseDouble( age ) ) ) * selectedItemValueFromSpinner ); 
    return answer;
}

private Double getSelectedItemValueFromSpinnerText(String spinnerText) {
    return spinnerValues.get(spinnerText);
}

Don't forget to include your existing code at : //Your Code Goes here... 不要忘记在以下位置包含现有代码://您的代码在这里...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM