微调器视图显示空白数据

时间:2016-02-22 11:01:46

标签: android android-layout spinner android-spinner

我在查看XML中的Spinner数据时遇到麻烦。 Toast可以在所选项目中显示数据,但在spinner视图中无法显示所有项目。

我的代码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_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>

感谢。

2 个答案:

答案 0 :(得分:0)

simple_spinner_dropdown_item布局里面没有'lbl_category'id。

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

答案 1 :(得分:0)

试试这个:

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