MySQL数据库中的动态微调器值

时间:2018-03-07 15:52:17

标签: android

我正在尝试动态设置微调器值。这些值将从MySql数据库中获取,并将显示在微调器中。以下是我的代码,直到现在。请让我知道我的错误是什么。

我的JSON回复

    {
        "result": [
        {
            "sl": "1",
            "lvl_name": "admin",
            "lvl_id": "10"
        },
        {
            "sl": "2",
            "lvl_name": "Moderator",
            "lvl_id": "1"
        },
        {
            "sl": "3",
            "lvl_name": "Teacher",
            "lvl_id": "2"
        },
        {
            "sl": "4",
            "lvl_name": "Student",
            "lvl_id": "3"
        }
    ]
}

我用来获取数据的JAVA代码。

public void getSpinnerData(){

    connUrl = "http://192.168.1.102/cms/spinner.php";

    StringRequest stringRequest = new StringRequest(connUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getStudents(JSONArray j){
    //Traversing through all the items in the json array
    for(int i=0;i<j.length();i++){
        try {
            //Getting json object
            JSONObject json = j.getJSONObject(i);

            //Adding the name of the student to array list
            arrayList.add(json.getString(TAG_USERNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    lvl_spinner.setAdapter(dataAdapter);


}

数据不显示在微调器中。它不起作用。请帮忙。

0 个答案:

没有答案