3

This is my response and I am trying to get into my spinner but its not showing can any one tell me what is mistake in my code..my snippet code is given below..and i also added response..Thanks in advance and sorry for bad English

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();
                        }
                });

            }
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96

2 Answers2

1

If you want to show items in dialog. set single choice items for the dialog

new AlertDialog.Builder(MContext)
                .setSingleChoiceItems(options, selectedIndex, itemClickListener)
                .setPositiveButton(MContext.getString(R.string.dialog_done), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        dialog.dismiss();
                    }
                })
                .setNegativeButton(MContext.getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

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

Where options should be CharSequence[] of country names

Deniz
  • 12,332
  • 10
  • 44
  • 62
1

Currently you are not passing Country names to Adapter

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
}

And if you want to pass data as data-source to ArrayAdapter then you should need to create custom adapter by extending ArrayAdapter

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • i need to learn this.. can you help http://stackoverflow.com/questions/29713778/after-selection-of-image-from-gallery-is-not-set-in-gridview/29714344#29714344 – Aditya Vyas-Lakhan Apr 18 '15 at 07:39
  • @perfection: see my answer in your question. let me know if still need help – ρяσѕρєя K Apr 18 '15 at 08:57
  • @ρяσѕρєяK i tried this way with spinner..but have some issue http://stackoverflow.com/questions/33865282/how-to-get-json-object-as-per-spinner-selection – albert Nov 23 '15 at 07:14
  • @ρяσѕρєяK can you help here http://stackoverflow.com/questions/33951442/how-to-open-facebook-sharing-android – Aditya Vyas-Lakhan Nov 27 '15 at 08:56