0

I am trying to fetch data from server into my spinner but its not showing any item in spinner , and i am not getting any logcat error also..i have taken this from 1 example , in my json output i want to fetch only name of country ..but its not showing anything:

this is my java class:

   pmcountry = (Spinner) findViewById(R.id.country);



        //citySpinner = (Spinner) findViewById(City);
        //locationSpinner = (Spinner) findViewById(R.id.Location);
        pmcountry .setOnItemSelectedListener(this);

        country_list = new ArrayList<String>();

        //location_list = new ArrayList<String>();
        // city_list = new ArrayList<String>();

        getData();
    }
    private void getData(){
        StringRequest stringRequest = new StringRequest(Config.DATA_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        JSONObject j = null;
                        try {
                            Log.d("Test",response);
                            JSONArray result = new JSONArray(response);
                            //Calling method getCountry to get the Country from the JSON Array
                            getCountry(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }});
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //Adding request to the queue
        requestQueue.add(stringRequest);
    }
    private void   getCountry(JSONArray  jsonArrayCountry){
        //Traversing through all the items in the json array
        List<Country> countries = new ArrayList<>();
        try {
            String country_name, country_code;
            JSONObject countries_object;
            for (int i = 0; i < jsonArrayCountry.length(); i++) {
                countries_object = jsonArrayCountry.getJSONObject(i);
                country_code = countries_object.getString("id");
                country_name = countries_object.getString("Name");
                countries.add(new Country(country_code, country_name));
            }
            /*ArrayAdapter countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
            pmcountry.setPrompt("--Select Country--");
            pmcountry.setAdapter(countryAdapter);
            pmcountry.setAdapter(new NothingSelectedSpinnerAdapter(countryAdapter,
                    R.layout.contact_spinner_row_nothing_selected,this));*/
            pmcountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                }
                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            });

        } catch (JSONException e) {

        }
    }

2 Answers2

1

You are trying to set the list of Custom Objects List<Country> By default array adapter takes list of String. List<String>.

You have to tweak your code to set List of Custom objects in spinner.

Android: How to bind spinner to custom object list?

This should solve your problem.

Community
  • 1
  • 1
Syed Taruf Naqvi
  • 507
  • 3
  • 18
  • but its working in my other project , i just tried with this json output..1 more thing in output there is only Name given ..whereas i am trying with id also..if i try to remove country_code..its showing error – user7316606 Feb 12 '17 at 06:13
0

Uncomment your , You are not setting data to spinner anywhere..

ArrayAdapter countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries);
        pmcountry.setPrompt("--Select Country--");
        pmcountry.setAdapter(countryAdapter);
        pmcountry.setAdapter(new NothingSelectedSpinnerAdapter(countryAdapter,
                R.layout.contact_spinner_row_nothing_selected,this));
N J
  • 27,217
  • 13
  • 76
  • 96