在spinner2的监听器中解析spinner1的当前项

时间:2014-07-25 16:06:08

标签: android android-spinner

如何在spinner2的onItemSelected侦听器中获取spinner1的位置,以便我可以转换为字符串并进行解析以进行计算?

我目前正在尝试在spinner1中调用adapterSpinner2.getItemAtPosition()但是得到一个:无效的浮点数:“”logcat错误。在我尝试调用adapterSpinner2的行上。

我知道我调用的数组都是数字,所以它不应该只是一个空字段错误。我在spinner1的onItemSelected侦听器中解析spinner1的工作正常,所以我相信它在我试图调用spinner2的位置。

对不起,如果有点混乱,我们将非常感谢任何帮助。

public class FragmentA extends Fragment implements AdapterView.OnItemSelectedListener{

Spinner atmosphereSpinner;
Spinner pressurelossSpinner;

String AtmosphereSpinner;
String PressureLossSpinner;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);

    atmosphereSpinner = (Spinner) view.findViewById(R.id.atmosphereSpinner);
    pressurelossSpinner = (Spinner) view.findViewById(R.id.pressurelossSpinner);

    final ArrayAdapter<CharSequence> adapterAtmosphere = ArrayAdapter.createFromResource(getActivity(), R.array.atmosphere_array, android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> adapterPressureLoss = ArrayAdapter.createFromResource(getActivity(), R.array.pressure_loss_array, android.R.layout.simple_spinner_item);

    atmosphereSpinner.setAdapter(adapterAtmosphere);
    pressurelossSpinner.setAdapter(adapterPressureLoss);

    atmosphereSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l){


            AtmosphereSpinner = adapterView.getItemAtPosition(pos).toString();
            float atmosphere = Float.parseFloat(AtmosphereSpinner);

            PressureLossSpinner = pressurelossSpinner.getItemAtPosition(pos).toString();
            float pressureloss = Float.parseFloat(PressureLossSpinner);


            if (Boolean is True){
              //Do calculation
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }

    });

    pressurelossSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l){

            AtmosphereSpinner = atmosphereSpinner.getItemAtPosition(pos).toString();
            float atmosphere = Float.parseFloat(AtmosphereSpinner);

            PressureLossSpinner = adapterView.getItemAtPosition(pos).toString();
            float pressureloss = Float.parseFloat(PressureLossSpinner);

            if (Boolean is true){
             //Do calculation
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }

    });

logcat的:

07-25 11:53:16.142      858-858/com.example.jstazak.ctsleaktester E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NumberFormatException: Invalid float: ""
            at java.lang.StringToReal.invalidReal(StringToReal.java:63)
            at java.lang.StringToReal.parseFloat(StringToReal.java:289)
            at java.lang.Float.parseFloat(Float.java:300)
            at com.example.FragmentA$2.onItemSelected(FragmentA.java:97)
            at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
            at android.widget.AdapterView.access$200(AdapterView.java:49)
            at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Line 63 is this line:                 PressureLossSpinner = pressurelossSpinner.getItemAtPosition(pos).toString();

1 个答案:

答案 0 :(得分:0)

在第二个微调器onItemSelected中执行此操作

AtmosphereSpinner = atmosphereSpinner.getSelectedItem().toString();

您可以获取所选的第一个微调器的值

而不是

AtmosphereSpinner = atmosphereSpinner.getItemAtPosition(pos).toString();

编辑:

1.Place String to try catch in float catch。如果发生异常,它将进入catch块。指定那些浮点数0.0f,然后尝试将其转换为double而不是float

2.在微调器'String

中编译floatonItemSelected转换的代码段
atmosphereSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l){
            float atmosphere; // It is better if you declare these 2 variables globally
            float pressureloss ;

            AtmosphereSpinner = atmosphereSpinner.getSelectedItem().toString();
            if(!AtmosphereSpinner.equalsIgnoreCase(""))
                atmosphere = Float.parseFloat(AtmosphereSpinner);
            else
                atmosphere = 0.0f;

            PressureLossSpinner = pressurelossSpinner.getSelectedItem().toString();
            if(!PressureLossSpinner.equalsIgnoreCase(""))
                pressureloss = Float.parseFloat(PressureLossSpinner);
            else
                pressureloss = 0.0f;

            if (Boolean is True){
              //Do calculation
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }

    });
  

对第二个微调器也这样做