javaandroidnumberpickerandroid-number-picker

Changing displayed values of NumberPicker


I want to change my first NumberPicker values whan user changes the second NumberPicker current value index. I've tried this code to achieve that:

weightPostfixNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker numberPicker, int oldValue, int newValue) {
            switch (numberPicker.getValue()) {
                case 0:
                    setNewValues(weightKgArray);
                case 1:
                    setNewValues(weightLbArray);
            }
        }
    }); //...
    private void setNewValues(String[] values) {
        weightNumberPicker.setDisplayedValues(null);
        weightNumberPicker.setMinValue(0);
        weightNumberPicker.setMaxValue(values.length-1);
        weightNumberPicker.setWrapSelectorWheel(false);
        weightNumberPicker.setDisplayedValues(values);
    }

But it works only once, and when I'm changing weightPostfixNumberPicker's current value index again, nothing happens. I've tried different listeners also, but result is the same. What I'm doing wrong here? Thanks in advance!


Solution

  • My fault was that I forget to add break/return after each case. After adding break it works like a charm.