2015-11-03 77 views
1

我创建微调从json的适配器数据微调。但是,为什么微调不显示文本,并选择价值时不显示。有人可以帮助我。Android微调不显示文本和选择时

微调

<Spinner android:id="@+id/spinCity" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="15dp" 
      android:textColor = "#000000" 
      android:prompt="@string/city_prompt" 
/> 

负载JSON用于微调。

cityItems = new ArrayList<City>(); 
    cityNames = new ArrayList<String>(); 

    JsonObjectRequest jsonReq = new JsonObjectRequest(
      AppConfig.URL_CITY, null, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 
      VolleyLog.d(TAG, "Response: " + response.toString()); 
      if (response != null) { 
       try { 
        JSONArray feedArray = response.getJSONArray("result"); 

        for (int i = 0; i < feedArray.length(); i++) { 
         JSONObject feedObj = (JSONObject) feedArray.get(i); 

         City item = new City(); 
         item.setId(feedObj.getInt("id")); 
         item.setCity(feedObj.getString("city")); 

         cityItems.add(item); 
         cityNames.add(feedObj.getString("city")); 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d(TAG, "Error: " + error.getMessage()); 
     } 
    }){ 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headers = new HashMap<String, String>(); 
      String credentials = AppConfig.USER_API+":"+AppConfig.PASSWORD_API; 
      String auth = "Basic " 
        + Base64.encodeToString(credentials.getBytes(), 
        Base64.NO_WRAP); 
      headers.put("Authorization", auth); 
      return headers; 
     } 
    }; 
    VolleyController.getInstance().addToRequestQueue(jsonReq); 

    spinCity = (Spinner) findViewById(R.id.spinCity); 
    ArrayAdapter<String> cAdapter = new ArrayAdapter<String> 
      (SendReportActivity.this, android.R.layout.simple_spinner_item, cityNames); 

    cAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 


    spinCity.setAdapter(cAdapter); 
    spinCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(getApplicationContext(), 
        "selected", Toast.LENGTH_LONG) 
        .show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 

而这个屏幕截图当我跑。 enter image description here

+0

确保您在'cityNames'中获得了一些值。 – activesince93

回答

4

因为在第一次你cityNames是空的,它需要从响应收到刷新后的数据。所以,你需要拨打notifyDataSetChanged()来更新数据。

cityItems = new ArrayList<City>(); 
    cityNames = new ArrayList<String>(); 
    **final ArrayAdapter<String> cAdapter = new ArrayAdapter<String> 
       (SendReportActivity.this, android.R.layout.simple_spinner_item, cityNames);** 
JsonObjectRequest jsonReq = new JsonObjectRequest(
     AppConfig.URL_CITY, null, new Response.Listener<JSONObject>() { 

    @Override 
    public void onResponse(JSONObject response) { 
     VolleyLog.d(TAG, "Response: " + response.toString()); 
     if (response != null) { 
      try { 
       JSONArray feedArray = response.getJSONArray("result"); 

       for (int i = 0; i < feedArray.length(); i++) { 
        JSONObject feedObj = (JSONObject) feedArray.get(i); 

        City item = new City(); 
        item.setId(feedObj.getInt("id")); 
        item.setCity(feedObj.getString("city")); 

        cityItems.add(item); 
        cityNames.add(feedObj.getString("city")); 
       } 
       **cAdapter.notifyDataSetChanged();** 

      } catch (JSONException e) { 
       e.printStackTrace(); 


       } 
      } 
... 
+0

谢谢@约翰,这是工作。但是,我怎样才能向纺纱工展示Promt。我添加android:promt在微调,但promt不显示。也许有另一种方式? – user3089464

+0

提示用于在下拉弹出框中显示标题,而不是默认文本。这里有一些建议[链接](http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one/)。希望有所帮助!你也应该看到你喜欢的显示器[android:spinnerMode](http://developer.android.com/reference/android/widget/Spinner.html#attr_android:spinnerMode)。 – NamNH

0

Spinner很可能不知道您的Response.Listener中添加的数据。

你需要做的是这样

Spinner s = (Spinner) findViewById(R.id.spinCity); 
BaseAdapter a = (BaseAdapter) s.getAdapter(); 
if (a != null) 
    a.notifyDataSetChanged(); 

后的for循环你打电话cityNames.add(feedObj.getString("city"))