如何将TextView添加到当前布局,以便我可以填充我的微调器?

时间:2012-12-04 06:49:23

标签: java android android-layout

这是我想要的布局。只要我实际上没有尝试填充我的微调器,一切都很好。基于我在搜索中找到的内容,日志是我需要使用带有ArrayAdapter的TextView。在保留当前布局的过程中,我是如何做到这一点的 - 旋转器和顶部的提交按钮以及用于返回结果的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);

    }

master.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>

spinner.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>

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

而不是R.id.spinnerStart / R.id.spinnerEnd,您需要传递textView

编辑:

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

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