I have looked at a number of threads on how to get the number picker to change programmatically, and fire the OnValueChangedListener. None of them are working for me. What I need is for the number picker to skip over certain numbers if they are already taken to a free slot. My current 'solution' is this:
// Set up channel picker change event to update other items in the form
mPickerAddress.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
// If the user scrolls the number picker, update the other form items
// Check to see if this channel is already used, and inform user, and reset picker
int n;
boolean contained = false;
// Iterate over each custom value to see if channel is used
for (n = 0; n < 10; n++) {
if (mCustomValues[n][0] == i1) {
contained = true;
}
}
if (contained) {
// Check direction being moved, if moving positive, skip over that way and vice versa
// If positive...
if(i1 - i > 0){
pickerSkipper(mPickerAddress,true);
}
else{
pickerSkipper(mPickerAddress,false);
}
} else {
// Update address value
mCustomValues[mPickerChannel.getValue() - 1][mCustomAddress] = i1;
// Update has changed
hasChanged[mPickerChannel.getValue() - 1] = true;
}
}
});
Skipping method (code derived from other posts on this issue):
// Skips forward or backward if a value is already taken
public void pickerSkipper(NumberPicker picker, boolean skip){
Method method;
try {
// reflection call for
// higherPicker.changeValueByOne(true);
method = picker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
method.setAccessible(true);
if(skip)method.invoke(picker, true);
else method.invoke(picker, false);
} catch (final NoSuchMethodException e) {
e.printStackTrace();
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
} catch (final InvocationTargetException e) {
e.printStackTrace();
}
}
This or any other way of configuring the reflection of the method causes the program to eventually crash into an endless garbage collection routine and 'suspending all threads took: X.XXXms' errors.
At this point I don't need to use this way of doing it, I just want something that works. It can't be this hard to get a number picker to skip forward, or can it?
Thank you in advance.