2017-02-12 37 views
0

我想從服務器獲取數據到我的微調,但它沒有顯示任何項目在微調,我也沒有得到任何logcat錯誤也..我採取了這個從1個例子,在我的JSON輸出,我只想抓取國家的名字..但它不顯示任何:在android中沒有將服務器項目放入我的微調器中?

這是我的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>

您必須調整您的代碼以設置微調器中的自定義對象列表。

Android: How to bind spinner to custom object list?

這應該解決您的問題。

+0

,但它的工作在我的另一個項目中,我只是試着用這個JSON輸出.1更多的東西在輸出中只有名稱給出..而我嘗試使用ID也..如果我嘗試刪除country_code ..顯示錯誤 – user7316606

0

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

使用此從服務器獲取的所有數據之後

+0

我面臨的dropdownview資源 – user7316606

+0

錯誤它說無法解決梅索德 – user7316606

0

取消註釋你的,你是不是設定數據在任何地方微調器..

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

我試着用同樣的DAT但仍然沒有任何舒」 – user7316606

+0

也沒有我得到任何的logcat錯誤... – user7316606

+0

您能夠從服務器獲取數據? –

相關問題