Getting float / double from NumberPicker

I am trying to use the https://github.com/SimonVT/android-numberpicker library, and the link is https://developer.android.com/reference/android/widget/NumberPicker.html#getValue%28%29 Now both levels of API 11 NumberPicker returns an int value in the getValue method, and a numeric value SimonVT returns an int value.

But I set double values ​​in a set of numbers using the following code:

String[] nums = {"1","1.5","2","2.5","3","3.5","4","4.5","5","5.5","6","6.5","7","7.5","8","8.5","9"}; final NumberPicker listeningScorenp = (NumberPicker) findViewById(R.id.listeningScore); listeningScorenp.setMaxValue(nums.length-1); listeningScorenp.setMinValue(0); listeningScorenp.setWrapSelectorWheel(false); listeningScorenp.setDisplayedValues(nums); 

Now I am fixated on how to get float / double values ​​from NUmberPicker.

+2
source share
1 answer

I just tested your code. getValue () actually returns the index of the selected item (only when you set the displayed values). All you have to do is parse the string and you will have what you wanted.

 String[] nums = {"1","1.5","2","2.5","3","3.5","4","4.5","5","5.5","6","6.5","7","7.5","8","8.5","9"}; int index = listeningScorenp.getValue(); String val = nums[index]; float selectedFloat = Float.parseFloat(val); 
+4
source

All Articles