0

这是我想要得到的布局。只要我实际上不尝试填充我的微调器,一切都加载得很好。根据我在搜索中发现的内容和日志,我需要将 TextView 与 ArrayAdapter 一起使用。在保留当前布局的同时,我究竟是如何做到这一点的——微调器和顶部的提交按钮以及用于在微调器下方返回结果的 ListView 是我无法实现的。

这是我的 onCreate 方法。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.master);
        DBHelper db = new DBHelper(this);
        List<Stations> st = db.getAllStations();
        List<CharSequence> stations = new ArrayList<CharSequence>();
        setList(4, 9);
        for (int i = 0; i < st.size(); i++)
        {
            stations.add(st.get(i).getStation());
        }
        ListView list = (ListView) findViewById(R.id.list);
        Spinner s1 = (Spinner) findViewById(R.id.spinnerStart);
        Spinner s2 = (Spinner) findViewById(R.id.spinnerEnd);

        ArrayAdapter<CharSequence> SimpleSpinner1 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
        ArrayAdapter<CharSequence> SimpleSpinner2 = new ArrayAdapter<CharSequence>(this, R.layout.spinner, R.id.textView1);
        SimpleAdapter nSchedule = new SimpleAdapter(this, departures, R.layout.row,
            new String[] {"train", "from", "to"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL, R.id.TO_CELL});
        for (CharSequence c : stations)
        {
            SimpleSpinner1.add(c);
            SimpleSpinner2.add(c);
        }
    list.setAdapter(nSchedule);
    s2.setAdapter(SimpleSpinner2);
    s1.setAdapter(SimpleSpinner1);

    }

大师.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Spinner
        android:id="@+id/spinnerStart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <Spinner
        android:id="@+id/spinnerEnd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinnerStart" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/spinnerEnd"
        android:text="Button" />

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        layout="@layout/listview" />

</RelativeLayout>

微调器.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="30dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>
4

2 回答 2

0

试试这个

// set id for spinner
spinner2 = (Spinner) findViewById(R.id.spinner2);
// prepare a list 
List<String> list = new ArrayList<String>();
    list.add("list 1");
    list.add("list 2");
    list.add("list 3");
// set adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter to spinner   
spinner2.setAdapter(dataAdapter);
于 2012-12-04T06:54:07.670 回答
0

而不是 R.id.spinnerStart/ R.id.spinnerEnd你需要传递一个 textView

编辑:

微调器 spinner1 = (Spinner)findViewById(R.id.spinner1); ArrayAdapter 适配器 = new ArrayAdapter(this, R.layout.spinner_item, R.id.textView1); spinner1.setAdapter(适配器);

textView1 存在于 spinner_item 布局中。希望这很清楚

于 2012-12-04T07:01:59.063 回答