Giter Club home page Giter Club logo

Comments (3)

RuiRoque-FA avatar RuiRoque-FA commented on May 26, 2024 2

I was having the exact same problem with a very erratic behavior. Found out that the component view is not reusable. Let me explain.

First, you need to have the exact order of:

numberPicker.setMinValue
numberPicker.setMaxValue
numberPicker.setDisplayedValues

If you invoke the "numberPicker.setDisplayedValues" first, which is a logic thing to do, you start getting a ArrayIndexOutOfBoundsException with a fixed index of 99. So, you do things in that order.

But the problem is when you try to reuse that same NumberPicker with different values. If the min or max value index is bigger than the displayed values list already there, you get again an ArrayIndexOutOfBoundsException.

But if the indexes are ok, the list is still all mixed up, with the selected value overlapping the other values, and sometimes even some missing values that only appear when we try to scroll through them.

The solution that I found was to set in the XML layout only a container for the NumberPicker (like a FrameLayout or something). And then, programmatically, create the NumberPicker and add it to the container. E.g.:

private void setNumberPicker(ViewGroup numberPickerContainer, String[] displayValues, String valueToSet) {
    NumberPicker numberPicker = new NumberPicker(getContext());

    numberPicker.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

    numberPicker.setSelectedTextColorResource(R.color.picker_selected_text_color);
    numberPicker.setSelectedTextSize(R.dimen.picker_selected_text_size);
    numberPicker.setTextColorResource(R.color.picker_text_color);
    numberPicker.setTextSize(R.dimen.picker_text_size);
    numberPicker.setDividerColorResource(android.R.color.transparent);

    final int DEFAULT_VISIBLE_ITEMS = 9;
    int itemsLength = displayValues.length;
    int numberOfVisibleItems = itemsLength < DEFAULT_VISIBLE_ITEMS ? (itemsLength + 1) : DEFAULT_VISIBLE_ITEMS;
    numberPicker.setWheelItemCount(numberOfVisibleItems);

    numberPicker.setMinValue(1);
    numberPicker.setMaxValue(displayValues.length);
    numberPicker.setDisplayedValues(displayValues);

    for (int i = 0; i < displayValues.length; i++) {
        if (displayValues[i].equals(valueToSet)) {
            numberPicker.setValue(i + 1);
            break;
        }
    }

    numberPickerContainer.removeAllViews();
    numberPickerContainer.addView(numberPicker);
}

Hope this helps others with the same problem.

from numberpicker.

stanleyguevara avatar stanleyguevara commented on May 26, 2024 1

+1'd the component being not reusable. I can't get it to work using any of workarounds here without at least graphical glitch (last item being on top before scrolling despite wrap around = false).

@ShawnLin013 this issue definitely should not be closed, it leads devs to believing that they're doing something wrong, so they reorder the min/max/val calls and do some weird workarounds, meanwhile it's the API here being fundamentally broken.

Don't get my wrong, I'm glad you reworked horrible original NumberPicker to point of being somewhat usable, but closing the issue just leads to people wasting time.

from numberpicker.

ShawnLin013 avatar ShawnLin013 commented on May 26, 2024

Hi @oenukr,
Thank you for your patch, but unfortunately there is an unacceptable side effect.
You can try the following code when received data from remote service.
I had tested it after the view is initialized 10 seconds later, and it's working correctly.

new CountDownTimer(10000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
         // do nothing
     }

    @Override
    public void onFinish() {
        int length = 10;
        String[] numbers = new String[length];
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = String.valueOf(i);
        }
        numberPicker.setMinValue(1);
        numberPicker.setMaxValue(length);
        numberPicker.setDisplayedValues(numbers);
        numberPicker.setValue(0);
    }
}.start();

from numberpicker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.