I have a number picker for setting a data limit in MB. right now, I have numberPicker, contains numeric values in sequence like [1,2,3,....., 2000 MB].
But I want a numberPicker that should contain numeric values like [100,200,300,...., 2000MB]. How can I do this?
Saif
Displayed array values for your picker
int NUMBER_OF_VALUES = 20; //num of values in the picker
int PICKER_RANGE = 100;
...
String[] displayedValues = new String[NUMBER_OF_VALUES];
//Populate the array
for(int i=0; i<NUMBER_OF_VALUES; i++)
displayedValues[i] = String.valueOf(PICKER_RANGE * (i+1));
/* OR: if the array is easy to be hard-coded, then just hard-code it:
String[] displayedValues = {"100", "200", "300", .....}; */
Set arr in your picker :
numPicker.setMinValue(0);
numPicker.setMaxValue(displayedValues.size()-1);
numPicker.setDisplayedValues(displayedValues);
get/set the value of the picker :
//To get the current value in the picker
choosenValue = displayedValues[numPicker.getValue()];
//To set a new value (let's say 150)
for( int i=0; i<displayedValues.length ; i++ )
if( displayedValues[i].equals("300") )
numPicker.setValue(i);
Try the code below
int start = 100;
String[] numbers = new String[20];
for(int i =0 ; i < 20 ; i++) {
numbers[i] = start + "";
start = start + 100;
}
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.id_number_picker);
numberPicker.setMaxValue(20);
numberPicker.setMinValue(1);
numberPicker.setDisplayedValues(numbers);
final String[] nums = new String[21];
for(int i=0; i<nums.length; i++) {
nums[i] = Integer.toString((i+1)*100);
}
numberPicker.setMinValue(0);
numberPicker.setMinValue(19);
numberPicker.setDisplayedValues(nums);
来源:https://stackoverflow.com/questions/34585648/how-to-customize-value-for-numbers-in-numberpicker-in-android