状态CIty使用微调器动态不工作

时间:2017-10-05 13:32:58

标签: android android-spinner

(可能重复) 我知道这可能是一个重复的问题,但问题与我在这里搜索的问题有所不同。

我正在尝试动态实现Spinner但无法实现它。

问题:我能够获取存储在stateID中的stateID然后传递给方法addCites(sID)但是这里发生的事情是当我选择状态时,状态微调器变得空虚,城市在连续循环中进入城市旋转器。我是否解决了这个问题。

 @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            testid.setText(getID(position));

            //getting stateID of selected state
            String sID = testid.getText().toString();

            //calling addCites Method
            addCities(sID);

            };

        public void addCities(String sID){

            final String stateID = sID;
            //Creating a string request
            StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.CITIES_URL,
                    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
                                cities = j.getJSONArray(Config.JSON_ARRAY_CITIES);

                                //Calling method getCities to get the students from the JSON Array
                                getCities(cities);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            //handle error here
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<>();
                    //Adding parameters to request
                    params.put(Config.KEY_STATE_ID, stateID);
                    //returning parameter
                    return params;
                }
            };
            //Adding the string request to the queue
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);

        }
        private void getCities(JSONArray k){
            //Traversing through all the items in the json array
            for(int i=0;i<k.length();i++){
                try {
                    //Getting json object
                    JSONObject json = k.getJSONObject(i);
                    //Adding the name of the student to array list
                    cityList.add(json.getString(Config.TAG_CITY_NAME));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            //Setting adapter to show the items in the spinner
            spinner_city.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, cityList));
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
           // error msg
        }

0 个答案:

没有答案