java - Android Spinner 获取所选项目

标签 java android

在我的Spinner中,我有一个String Array,用于它{久坐、轻度 Activity 、中度 Activity 和重度 Activity }(这是计算一个人的基础代谢率)

我想要做的是在Spinner中获取所选项目

例如,当我点击久坐时,久坐的值为1.2bmr<的结果/strong> 乘以1.2。但我似乎无法让它为我工作。

请检查下面我的代码:

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

最佳答案

请尝试修改以下代码:

创建一个字段:

private HashMap<String, Double> spinnerValues;

从 Activity 的 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);
}

然后找到修改后的函数进行计算,如下所示:

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

不要忘记在以下位置包含您现有的代码://Your Code Goes here...

关于java - Android Spinner 获取所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136953/

相关文章:

android - 'aapt' 错误。预编译器构建中止

android - 如何在 android 中使用 Dictionary(如 iphone NSDictionary)?

java - 如何在一行代码中获取和设置字符串数组?

java - 在 DJigger 中搜索主机名没有找到任何内容

android.mk arm-linux-androideabi-g++ 异常和 __cxa_allocate_exception

android - 无需身份验证即可使用 google firebase 存储

java - Android:每秒自动获取下一个列表项

java - 应该如何查询它是否有效? ElasticSearch Java API

java - Spring Boot 项目 - 编写 JPQL (Intellij) 的问题

java - 基本递归方法——阶乘