Android Spinner不显示文本和选择时

时间:2015-11-03 11:01:33

标签: android json android-spinner android-volley

我使用json中的适配器数据创建微调器。但是为什么微调器没有显示文本而在选择值时不显示。有人可以帮帮我。

微调

<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

2 个答案:

答案 0 :(得分:5)

因为第一次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();


              }
            }
 ...

答案 1 :(得分: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"))