Parsing spinner1's current item inside of spinner2's listener


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


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


In second spinner onItemSelected do this

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

You can get the value of first spinner which is selected

Instead of

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

<strong>EDIT :</strong>

1.Place String to float conversion in try catch. If exception occur it will go to catch block. There assign those floats 0.0f else try to convert it double instead of float.

2.Code snippet for String to float conversion in spinners' onItemSelected

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

Do the same for second spinner also



  • Issue with fragment transaction and passing values to fragment?
  • How to save state of CheckBox while scrolling in ListView?
  • Fragment tabhost is not working in Fragment
  • Binary XML file line #26: Duplicate id, tag null, or parent id with another fragment
  • What other Adapters can I use for ListView?
  • List view crashes when scrolling “The application may be doing too much work on its main thread.”
  • Listview with multiple strings
  • Drag and Drop for imageview not working
  • Where are files stored on an Android emulator?
  • android-How to enable cache or buffering when videoView have downloaded a video
  • Is the listview recycling early? Or is Universal ImageLoader canceling in error? Or is my Logic wron
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • Variant from android-autofittextview library : scaling makes strange behaviour
  • Listview - list item selection and initial setting (custom made items)
  • Replace Fragment with another on back button
  • How to create a new Bundle object?
  • Activity overlaying. WindowManager.LayoutParams
  • How to Make a Spinner (In a Fragment) That Changes the App's Language?
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • C#: Import/Export Settings into/from a File
  • How to get listview position?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • WPF version of .ScaleControl?
  • How can I display the parent menu item's description using Wordpress walkers?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Connect .sks to skscene.h
  • How to test if a URL from an Eclipse bundle is a directory?
  • Email format validation in mvc3 view
  • Projection media query: browser support and workarounds?
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Deleting and Updating values from a cusrsor adapter
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • align graphs with different xlab
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?