微调器视图显示空白数据

Spinner View Displaying Blank Data

我无法在 XML 中查看我的 Spinner 数据。 Toast 可以显示所选项目中的数据,但在微调器视图中无法显示所有项目。

我的代码Java

public class LoadCategoryTask extends AsyncTask<Void, Void, Boolean> {
    LoadCategoryTask() {
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return false;
        }

        List<NameValuePair> data = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_get_category, "GET", data);

        Log.d("All Category: ", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                category = json.getJSONArray(TAG_CATEGORY);

                for (int i = 0; i < category.length(); i++) {
                    JSONObject c = category.getJSONObject(i);

                    String id = c.getString(TAG_ID_CATEGORY);
                    String category = c.getString(TAG_CATEGORY);

                    HashMap<String, String> map = new HashMap<String, String>();
                    System.out.println(id + ", " + category);
                    map.put(TAG_ID_CATEGORY, id);
                    map.put(TAG_CATEGORY, category);

                    category_arraylist.add(map);
                }
            } else {
                System.out.println("Category not found");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
        showProgress(false);

        if (success) {
            simpleAdapter = new SimpleAdapter(PlacesActivity.this, category_arraylist, R.layout.adapter_spinner_category,
                    new String[] { "category" },new int[]{R.id.lbl_category});
            simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {

                public boolean setViewValue(View view, Object data,String textRepresentation) {
                    TextView textView = (TextView) view;
                    textView.setTextColor(Color.BLACK);
                    textView.setText(textRepresentation);
                    return true;
                }
            };
            simpleAdapter.setViewBinder(viewBinder);
            sp_category.setAdapter(simpleAdapter);
            sp_category.requestFocus();
        }
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
        showProgress(false);
    }
}

而这个XML代码Activity,activity_places.xml

<Spinner
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/sp_categoriy"
   android:theme="@style/Base.Widget.AppCompat.Spinner.Underlined" />

此 XML 适配器微调器代码,adapter_spinner_category.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/lbl_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/navigationBarColor"/>

</LinearLayout>

谢谢。

simple_spinner_dropdown_item 布局内部没有 'lbl_category' id。

制作您的自定义 DropDownView 布局(只需复制 simple_spinner_dropdown_item)并将 ID 'lbl_category' 设置为其 CheckedTextView。

试试这个:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(Contex,   R.layout.spinner_textview, ArrayOfElements);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);