Search code examples
androidtype-conversiontostringnumberpicker

Converting the value from NumberPicker to String


I was trying to get the selected value from a NumberPicker, np_hr using getValue() and convert it to string using toString() but the error cannot resolve method 'toString()' keeps appearing. Is that method not allowed for number pickers? I've tried converting values to string from other components such as spinner and its working fine.

Here's what the code looks like

// -- Number Picker
    final NumberPicker np_hr = (NumberPicker) findViewById(R.id.Hrtime_numpicker);
    final NumberPicker np_min = (NumberPicker) findViewById(R.id.minTime_numpicker);
    np_hr.setMinValue(00);
    np_hr.setMaxValue(24);
    np_hr.setWrapSelectorWheel(true);

Here is the function where the value will be obtained and converted

                @Override
                protected Map<String, String> getParams() throws AuthFailureError{
                    Map<String,String> parameters = new HashMap<String, String>();
                    parameters.put("sched_time",np_hr.getValue().toString()); 

                    return parameters;
                }


            };
            requestQueue.add(request);
        }
    });

Solution

  • Try this code:

     Map<String,String> parameters = new HashMap<String, String>();
     int value = np_hr.getValue();
     parameters.put("sched_time",Integer.toString(value));  //pass as string 
    

    here NumberPicker.getValue() returns an integer value , to convert it to string you need to use Integer.toString(value)