Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android spinner获取选定项_Java_Android - Fatal编程技术网

Java android spinner获取选定项

Java android spinner获取选定项,java,android,Java,Android,在我的微调器中,我有一个字符串数组{久坐、轻度活动、中度活动和重度活动}这是一个人BMR的计算 我要做的是在微调器中获取选定项 例如,当我单击“久坐”时,因为“久坐”的值为1.2,所以bmr的结果将乘以1.2。但我似乎无法让它为我工作 请务必检查以下我的代码: private void calculateMen(){ String f1 = etft.getText().toString(); String f2 = etin.getText().toString();

在我的微调器中,我有一个字符串数组{久坐、轻度活动、中度活动和重度活动}这是一个人BMR的计算

我要做的是在微调器中获取选定项

例如,当我单击“久坐”时,因为“久坐”的值为1.2,所以bmr的结果将乘以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 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);
}
创建一个字段:

private HashMap<String, Double> spinnerValues;

不要忘了将现有代码包含在://您的代码放在这里…

要检查您的代码,您应该发布日志,因为在您的代码中没有任何微调器。发布一些相关的代码,或者整个活动是编译还是运行时错误…我还没有尝试运行这些代码。如果它的编译错误,希望你能修复它。如果运行时出错,请共享logcat输出,以便我可以帮助您08-27 14:52:33.456:E/AndroidRuntime337:at com.fps.iHealthFirst.BMRCalculator.getAnswerBMRCalculator.java:76有什么异常?你能从logcat复制粘贴更多信息吗…我是指红色的完整堆栈跟踪
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);
}