Getting float/double from NumberPicker

六月ゝ 毕业季﹏ 提交于 2019-12-02 02:15:47

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/18464571/getting-float-double-from-numberpicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!