1

我的列表视图有一个自定义布局项。布局项中有一个微调器,需要用值填充,通常是通过android:entries
这种方法我遇到的问题是最终用户无法修改值,这是我想要包括的内容。由于布局项和随后的微调器在同一个列表视图上重复多次,我想必须有一种方法以编程方式填充它一次。我就是想不通。

XML:

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/exerciselbl" />

        <Spinner
            android:id="@+id/Exercise"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/workout_items" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/repslbl" />

        <Spinner
            android:id="@+id/Reps"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/reps_count" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/weightlbl" />

        <EditText
            android:id="@+id/Weight"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />
    </LinearLayout>

</LinearLayout>

所以是的,我想避免使用android:entries="@array/workout_items",因为这意味着在 XML 资源文件中手动输入微调器的每个项目,并且我无法在程序运行时动态添加项目。

4

1 回答 1

0

Here is a simple example of how it would be done, obviously the generics can change and there are other ways of loading the spinner.

public void populateSpinner () {
    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    List<CharSequence> list = new ArrayList<CharSequence>();
    for (int i = 0; i < 10; i++) {
        list.add("Item " + i);
    }
    adapter.addAll(list);
    adapter.notifyDataSetChanged();
}

One of the more important lines is adapter.notifyDataSetChanged(), this is because it tells the view that it needs to refresh the data associated with this spinner.

For more information, see the ArrayAdapter and Spinner classes.

于 2012-12-16T14:36:30.633 回答