2017-02-12 5 views
0

私のスピナーにサーバーからデータを取得しようとしていますが、スピナーのアイテムが表示されていません。また、logcatエラーも発生していません。これは1つの例から取ったものです。国の名前は、その何も表示されない..but:アンドロイドで私のスピナーにサーバー項目を渡していないのですか?

これは私の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出力でもっと名前が与えられて.. country_code..itsがエラーを表示しています – user7316606

0

pmcountry.setAdapter(countryAdapter)。 pmcountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);サーバーからすべてのデータをフェッチした後

+0

私はそれは私もDATと試みたが、まだ何も「 – 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

を解決できないと言うdropdownviewリソース – user7316606

+0

をshowinも私は、任意のlogcatエラーを取得していないMethodeの – user7316606

+0

は、サーバーからデータを取得することができますか? –

関連する問題