Making numberpicker with usernames picked from database by the volley library

50 views Asked by At

I'm trying to make a number picker with usernames picked from database by the volley library. My php script's response looks like this:

[{"username":"adam"},{"username":"tomasz"},{"username":"komercja"}]

My number picker code look like this

        JSONArray arr = null;
        try {
            arr = new JSONArray(responsefromphp);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        List<String> list = new ArrayList<String>();
        for(int i = 0; i < arr.length(); i++){
            try {
                list.add(arr.getJSONObject(i).getString("username"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        picker.setDisplayedValues(null);
        picker.setMinValue(0);
        picker.setMaxValue(arr.length()-1);
        String [] userlist = list.toArray(new String[1]);
        picker.setDisplayedValues(userlist);

and if i hardcode my response from php script as string

String responsefromphp = [{"username":"adam"},{"username":"tomasz"},{"username":"komercja"}]

My number picker works perfectly, but when I try to get data from php script by using the volley library I have to put the number picker code inside of volley onresponse and when I run the activity the number picker shows 'n' as first pick. The other values are still ok, and if I move the number picker up and down 'n' changes to the first username.

I don't understand why this 'n' value is here and want to get rid of it.

I thought of saving my response from volley to a String variable outside of volley and then putting it into my number picker code, but if I'm trying to set the String variable inside volley's onresponse I cant use it outside.

How can I remove this 'n' value from my data?

0

There are 0 answers