0

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.

tommyvdp
  • 41
  • 6
  • http://stackoverflow.com/questions/23089868/how-do-i-skip-some-number-from-number-picker This can help you i think – Mukeshkumar S Apr 20 '17 at 14:17
  • Thanks for your input. I don't want to change the displayed values. I want the number picker to evaluate if a value is already 'taken' by another input, and if so, skip over it until there is a free value to rest on. It is a list of channels that can only be taken once. I suppose I could try to delete the taken values from the picker, however... – tommyvdp Apr 20 '17 at 14:24
  • 'taken' by another input.... If you have a number or array you have to compare and omit that's the way of doing to my knowledge. Sorry If I wasted your time – Mukeshkumar S Apr 20 '17 at 14:54
  • Thanks, I will try that, it was a good suggestion. I wanted the picker to visibly 'skip' over the taken values and for them to remain there, but if that will not work, this probably will be fine. – tommyvdp Apr 20 '17 at 15:04
  • Lemme know if worked and also update your answer – Mukeshkumar S Apr 20 '17 at 15:47

0 Answers0