我使用来自json的适配器数据创建spinner。但是为什么微调器不显示文本,并且在选定值时不显示。请有人帮我。
纺纱机

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

        }
    });

我跑步的时候拍下了这个画面。
android - Android微调器不显示文本,并且处于选中状态-LMLPHP

最佳答案

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


              }
            }
 ...

关于android - Android微调器不显示文本,并且处于选中状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33497458/

10-10 07:52