在Spinner OnItemSelected上调用JSON并将数据显示到另一个Spinner中

时间:2013-08-15 11:36:28

标签: android json android-spinner

我正在制作一个Android应用程序,其中我使用两个微调器。在第一个微调器中,我正在显示来自JSON的数据,我已经成功完成了。现在,通过单击第一个微调器的项目,我需要将来自另一个JSON服务的数据显示到第二个微调器。

第一次服务(我在第一个微调器上显示此服务的city_name):

{"result":{"data":[{"city_id":"16","city_name":"\u00c4ngelholm"},
{"city_id":"23","city_name":"B\u00e5stad"},
{"city_id":"22","city_name":"Halmstad"},
{"city_id":"19","city_name":"H\u00f6gan\u00e4s"},{"city_id":"20","city_name":"Helsingborg"},
{"city_id":"15","city_name":"Klippan"},
{"city_id":"24","city_name":"Kungsbacka"},
{"city_id":"21","city_name":"Laholm"},{"city_id":"18","city_name":"Landskrona"}],
"status":"true","description":""}} 

第二次服务:

{"result":{"data":[{"category_id":"18","category":"Aff\u00e4rsverksamhet",
"city_id":"16","city_name":"\u00c4ngelholm"},{"category_id":"19","category":"\u00d6vrigt",
"city_id":"16","city_name":"\u00c4ngelholm"},{"category_id":"13","category":"Bostad",
"city_id":"16","city_name":"\u00c4ngelholm"},{"category_id":"15","category":"Elektronik",
"city_id":"16","city_name":"\u00c4ngelholm"},{"category_id":"12","category":"F\u00f6r hemmet","city_id":"16","city_name":"\u00c4ngelholm"},{"category_id":"11","category":"Fordon",
"city_id":"16","city_name":"\u00c4ngelholm"},
{"category_id":"16","category":"Fritid & Hobby",
    "city_id":"16","city_name":"\u00c4ngelholm"}],
    "status":"true","description":""}}

正如您在服务city_idcity_name中看到的那样,是常见字段。如果我从第一个微调器中选择city_name,它将通过city_idcity_name匹配,并针对该category显示city_name

以下是我尝试的代码。

 private class AllCities extends AsyncTask<String, String, JSONObject>
    {
        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
        }

        @Override
        protected JSONObject doInBackground(String... params) 
        {
            try
            {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost("url of First Service");

                HttpResponse resp = client.execute(post);

                HttpEntity entity = resp.getEntity();

                String response = EntityUtils.toString(entity);

                return new JSONObject(response);

            }

            catch(Exception ex)
            {
                ex.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(JSONObject result) 
        {
            super.onPostExecute(result);


            if(result != null)
            {
                myList = new ArrayList<HashMap<String,String>>();

                if(! result.has("false"))
                {
                    try
                    {
                        JSONObject object = result.getJSONObject("result");


                        JSONArray array = object.getJSONArray("data");

                        stringArray = new ArrayList<String>();

                        for(int i=0; i<array.length(); i++)
                        {   
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("city_id", array.getJSONObject(i).getString("city_id"));                            
                            map.put("city_name", array.getJSONObject(i).getString("city_name"));
                            myList.add(map);

                            stringArray.add(array.getJSONObject(i).getString("city_name"));


                        }

                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(PostAdds.this, 
                                android.R.layout.simple_spinner_item, stringArray);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spinner_city.setAdapter(adapter);

                    }

                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }

                }

            }

            else if(result == null)
            {
                Toast.makeText(PostAdds.this, 
                        "Hittade inga Detaljer Vänligen Kontrollera din Internet-anslutning", 
                        Toast.LENGTH_LONG).show();
            }

        }
    }

 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    {   
        //what am I suppose to do here????

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) 
    {


    }

1 个答案:

答案 0 :(得分:0)

对于迟到的答案感到抱歉。我自己解决了这个问题,但从未回到这里发布适用于我的解决方案。现在我发布这个答案,如果有任何人遇到同样的问题可以从这里得到帮助。

所以我做了什么

1)调用第一个服务并在第一个微调器上填充城市名称,当选择第一个微调器的项目时,将其值保存在全局字符串中,如

String selectedCity; selectedCity = spinner1.getSelectedItem().toString();

2)当点击第一个微调器的任何项目时,调用第二个服务并在填充第二个微调器时进行简单检查。

    ArrayList<String> categories = new ArrayList<String>();
    try {
           JSONObject object = result.getJSONObject("result");
           JSONArray array = object.getJSONArray("data");

           for(int a = 0; a < array.length(); a++) {
                 String cityName = array.getJSONObject(a).getString("city_name");
                if(cityName.equals(selectedCity)) // this check is important
                {
                        categories.add(cityName );
                }
          }
         // after for loop ends populate adapter of second spinner 
         ArrayAdapter<String> adapt = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, categories);
         spinner2.setAdapter(adapt);
    }
    catch(Exception e) {
          e.printStackTrace();
    }

通过这种方式,我获得了理想的结果。还有其他解决方案,这个问题就像其中一个就是当你调用第二个服务时传递city_id或city_name以及url作为params,service只会返回你想要的类别。

希望这会解决任何一个有类似问题的人。我今天只检查了我的Stackoverflow个人资料,发现这个问题没有得到任何人的回答,所以我自己做了:)