2017-02-12 17 views
0

Я пытаюсь извлечь данные с сервера в свой счетчик, но его не покажет какой-либо элемент в spinner, и я тоже не получаю ошибку logcat. Я взял это из 1 примера, в моем json-выходе хочу получить только название страны ..но его не показывая ничего:не получает элемент сервера в моем счетчике в андроиде?

это мой Java класс:

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) { 

     } 
    } 

ответ

1

Вы пытаетесь установить список пользовательских объектов List<Country> По адаптер массива по умолчанию принимает список String. List<String>.

Вы должны настроить свой код для установки списка пользовательских объектов в spinner.

Android: How to bind spinner to custom object list?

Это должно решить вашу проблему.

+0

, но он работает в моем другом проекте, я просто попытался с этим выходом json. Еще одна вещь в выходе есть только имя, данное .. где я тоже пытаюсь с id.если я пытаюсь удалить country_code..its показывает ошибку – user7316606

0

pmcountry.setAdapter (countryAdapter); pmcountry.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);

Используйте это, после получения всех данных от сервера

+0

я столкнулся ошибку на dropdownview ресурсы – user7316606

+0

он говорит не может разрешить METHODE – user7316606

0

Раскоментируйте вашей, Вы не данные настройки в любом месте Spinner ..

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)); 
+0

я попытался с Дат также, но до сих пор ничего» – user7316606

+0

не showin ни я получаю какую-либо ошибку LogCat ... – user7316606

+0

смог получить данные с сервера Вы? –