android - 如何在微调器中获取 json 响应?

标签 android json android-spinner

这是我的回复,我正在尝试进入我的微调器,但它没有显示任何人都可以告诉我我的代码中有什么错误..我的代码 fragment 在下面给出..我也添加了回复..谢谢提前抱歉英语不好

enter image description here enter image description here

 {
   "status":"success",  
    "country_list":  
     [  
       {  
       "country_id":237,  
        "name":"Zimbabwe"  
       },  
       {  
        "country_id":236,   
        "name":"Zambia"  
       }  
     ]  
   }  

    class Country extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {

          ArrayAdapter<String> adaptercountry ;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(MyFragment.this.getActivity());
                pDialog.setMessage("Loading...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }
            protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
                ServiceHandler sh = new ServiceHandler();
                data = new ArrayList<HashMap<String, String>>();
                String jsonStr = sh.makeServiceCall(COUNTRY_URL, ServiceHandler.GET);

                Log.d("Response: ", "> " + jsonStr);
                if (jsonStr != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(jsonStr);

                        country_list = jsonObj.getJSONArray(COUNTRY_LIST);

                        for (int i = 0; i < country_list.length(); i++) {
                            JSONObject c = country_list.getJSONObject(i);


                            HashMap<String, String> map = new HashMap<String, String>();

                            map.put(COUNTRY_ID, c.getString(COUNTRY_ID));
                            map.put(COUNTRY_NAME,c.getString(COUNTRY_NAME));

                            data.add(map);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

                return data;
            }


            protected void onPostExecute(ArrayList<HashMap<String,String>> result) {

                super.onPostExecute(result);
                if (pDialog.isShowing())
                    pDialog.dismiss();

                adaptercountry = new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_spinner_dropdown_item);

                spcountry.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View w) {
                          new AlertDialog.Builder(getActivity())
                          .setTitle("Select")
                          .setAdapter(adaptercountry, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                spcountry.setText(adaptercountry.getItem(which).toString());

                              dialog.dismiss();
                            }
                          }).create().show();
                        }
                });

            }

最佳答案

目前您没有将国家/地区名称传递给适配器

protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
  super.onPostExecute(result);
    if (pDialog.isShowing())
       pDialog.dismiss();

    //prepare Array of country name
    String[] arrConuntry=new String[data.size()];
    for(int index=0;index<data.size();i++){
              HashMap<String, String> map=data.get(index);
          arrConuntry[index]=map.get(COUNTRY_NAME);
     }  
     // pass arrConuntry array to ArrayAdapter<String> constroctor :
    adaptercountry = new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_spinner_dropdown_item,
                                                              arrConuntry);
    ///.....your code here
}

如果你想将 data 作为数据源传递给 ArrayAdapter 那么你应该需要 create custom adapter by extending ArrayAdapter

关于android - 如何在微调器中获取 json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741395/

相关文章:

java - Android Activity LifeCycle,应用程序运行时运行 while 循环的方法是什么?

javascript - 使用 AJAX 和 JavaScript 在点击时显示模式

android - Spinner onItemSelected 被错误调用(没有用户操作)

android - 线性布局中的两个小部件一个挨着一个

android - 在 Android 上使用 Facebook API 创建自定义墙贴

java - 如何在谷歌分析中为屏幕浏览量应用过滤器

android - 实现 'com.github.prolificinteractive:material-calendarview:2.0' 无法解析

javascript - 为什么我无法使用 JSON.parse() 解析此 JSON 字符串

python - python中JSON对象的类型是什么?

android - Nexus 5/5x 上不显示微调器下拉箭头