Search code examples
androidnumberpicker

Getting float/double from NumberPicker


I am trying the https://github.com/SimonVT/android-numberpicker library and the reference is https://developer.android.com/reference/android/widget/NumberPicker.html#getValue%28%29 Now both, the API level 11 NumberPicker returns int value in getValue method and also the SimonVT numberpicker returns int value.

But I had set double values in the number picker by using 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 stuck with how to retrieve the float/double values from NUmberPicker.


Solution

  • I just tested using your code. getValue() actually returns the index of the item selected (only when you set the displayed values). All you'll need 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);