1

I am trying to use a set of numbers for my NumberPicker that consist of (-50 -> 50), the MinValue / MaxValue of the NumberPicker are set to 0 / 100 and then setValue as 50 for the default starting point (0), any idea why it displays the numbers properly except for -1 through -5... when not selected they display fine... when selected they display as -19, -29, -39, -49, -50.

I have tested the int array I am feeding it with and all the values in that are fine.

This is a snippet from the code I am using to populate the NumberPicker...

String[] currentScoreArray = new String[100];
final NumberPicker currentScore = (NumberPicker) findViewById(R.id.currentScore);
for (int i = 0; i <= 100; i++)
    currentScoreArray[i] = Integer.toString(i - 50);
currentScore.setMaxValue(100);
currentScore.setMinValue(0);
currentScore.setWrapSelectorWheel(false);
currentScore.setDisplayedValues(currentScoreArray);
currentScore.setValue(50);

After talking with baba tenor, I still cannot resolve this I have made a YouTube video of the issue and included the code and logcat in links on the video comment.

http://www.youtube.com/watch?v=bF39THktVY4

kyau
  • 886
  • 8
  • 5
  • Please post the code where you are handling and displaying the selections. – Erol Jul 03 '12 at 02:07
  • Are you referring to the XML that goes along with it? I am not using any code to handle and display the selections as NumberPicker is an android class (I am letting the android code do all that for me). – kyau Jul 03 '12 at 17:22
  • I do not get a few things about your code. First, why are you setting min and max values to 0 and 100 if you want your range to be [-50, 50]. Second, why don't you try running the for loop from -50 to 50 instead of dealing with (i-50) operation every time. – Erol Jul 03 '12 at 17:42
  • I tried what you said and changed the for loop around a bit (it was how it was due to how I was originally using the control). But like I said that which is the original problem here... "java.lang.IllegalArgumentException: minValue must be >= 0" is what I get when I try to do currentScore.setMinValue(-50); – kyau Jul 03 '12 at 18:01
  • Did you check currentScoreArray? Do you see all the numbers properly from -50 to 50? Also, what do you mean by "when not selected they display fine"? – Erol Jul 03 '12 at 18:12
  • I am outputting the array to the Log and it displays perfectly fine. `Log.e("err", Arrays.toString(currentScoreArray));` It is -50 through 50. What I mean by when not selected is the NumberPicker has a selected value or current and you can see faded out the next and previous values. when -1 through -5 is a previous values they display just as that -1 through -5, but then when i hit previous on the NumberPicker to select the -1 through -5 they change to -19, -29, -39, -49, -50 respectively while selected, then go back to -1 through -5 when de-selected. It only happens with these values. – kyau Jul 03 '12 at 18:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13379/discussion-between-baba-tenor-and-kyau) – Erol Jul 03 '12 at 18:21

2 Answers2

3

Old question but I post an idea for other users:

Maybe you could use a NumberPicker.Formatter.

Example:

final int minValue = -50
final int maxValue = 50
NumberPicker numberPicker = new NumberPicker(myActivity);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(maxValue - minValue);
numberPicker.setValue(myCurrentValue - minValue);
numberPicker.setFormatter(new NumberPicker.Formatter() {
    @Override
    public String format(int index) {
        return Integer.toString(index - minValue);
    }
});

then to get back the selected value:

int myNewValue = numberPicker.getValue() + minValue
alberthier
  • 643
  • 1
  • 7
  • 9
1

It seems like you need to implement your own NumberPicker class. You can start with the code here: http://www.quietlycoding.com/?p=5. It is apparently close to what the Google NumberPicker widget uses. The InputFilter set on the EditText object is what prevents negative numbers from being displayed. You can simply comment out the call to setFilters on the EditText object and it will display negative numbers to get you started. Digging into the InputFilter implementation can offer better solutions.

Kaleb
  • 1,855
  • 1
  • 18
  • 24