How can I get spinner1's position inside of spinner2's onItemSelected listener so that I can convert to a string and parse for a calculation?
I am currently attempting to call adapterSpinner2.getItemAtPosition() inside of spinner1 but am getting an: Invalid float: "" logcat error. on the line where I am trying to call adapterSpinner2.
I know that my array that I am calling is all numbers, so it should not be simply an empty field error. My parse of spinner1 inside of spinner1's onItemSelected listener works fine so I believe it is in where I am trying to call spinner2.
Sorry if that is a bit confusing, any help would be greatly appreciated.
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();