-1

I have a number picker set up with strings but I am having a problem with getting the string that is being displayed.

Here is the code for the construction of the number picker.

fractionPicker is my Number picker .

void fractionPickerFunction(){
    final String[] arrayString= new String[]{"None", "1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8"};

    fractionPicker.setMinValue(0);
    fractionPicker.setMaxValue(arrayString.length-1);

    fractionPicker.setFormatter(new NumberPicker.Formatter() {
        @Override
        public String format(int value) {

            return arrayString[value];
        }
    });
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 1
    What problem are you having? – shmosel Aug 12 '17 at 00:25
  • 1
    Please read [mcve] for tips about how to create a code example. – Code-Apprentice Aug 12 '17 at 00:26
  • I am trying to get the value of what the picker is on. I cant use getValue() because they are not ints. So I don't know what methods to use in order to get the actual value of the string – Jose Hermosillo Aug 12 '17 at 00:28
  • You could easily get values if you store your array in xml file and access it through getStringArray() and ArrayAdapter. Consider the given link, i hope it would help, https://stackoverflow.com/questions/2453989/help-in-getting-string-array-from-arrays-xml-file – Tehmina Aug 12 '17 at 01:27

2 Answers2

0

Set a change listener if you need to just get the result. If you need to modify the variable with the setFormatter still then just use both. call your setFormatter, then also add the change listener. It will then return to you, your modified variable.

Change

void fractionPickerFunction(){
    final String[] arrayString= new String[]{"None", "1/8", "1/4", "3/8", "1/2", "5/8", "3/4", "7/8"};

fractionPicker.setMinValue(0);
fractionPicker.setMaxValue(arrayString.length-1);

fractionPicker.setFormatter(new NumberPicker.Formatter() {
    @Override
    public String format(int value) {

        return arrayString[value];
    }
});

}

To this:

fractionPicker.setOnValueChangedListener(new OnValueChangeListener() {

        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            // TODO Auto-generated method stub

            String Old = "Old Value : ";
            String New = "New Value : ";

        }
    });
Dreamers Org
  • 1,151
  • 3
  • 12
  • 30
0

You should refer to the android documentation on the NumberPicker. In short, you are going to want to set a OnValueChangeListener by calling setOnValueChangedListener on your NumberPicker. You can do that with the following:

fractionPicker.setOnValueChangedListener(new OnValueChangeListener() {

    @Override 
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        // Do your work here 

        // Parameters
        //  picker  NumberPicker: The NumberPicker associated with this listener.
        //  oldVal  int: The previous value.
        //  newVal  int: The new value.
    }
});
Seabass77
  • 187
  • 5
  • 13