簡體   English   中英

具有包含LinearLayout的行的ListView

[英]ListView with rows containing a LinearLayout

我正在嘗試使用ListView實現一個列表,該列表包含使用LinearLayout構建的行。 此LinearLayout由一個復選框和一個textview組成。 該行的布局名為tasks_list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <CheckBox android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

然后從我的代碼中以這種方式初始化列表:

final String[] items_task = new String[] { "one",
                "two",
                "three" };

setListAdapter(new ArrayAdapter<String>(this, R.layout.tasks_list_row, items_task));

但隨后出現錯誤: ArrayAdapter-您必須為TextView提供資源ID

然后,我嘗試通過這種方式指定TextView ID:

setListAdapter(new ArrayAdapter<String>(this, R.id.text1, R.layout.tasks_list_row, items_task));

但是我收到一個“資源未找到”錯誤(R.java文件中存在R.id.text1)。

我該怎么辦?

在這種情況下,您需要使用android.R.id.text1,因為您引用的是內置的android ID(線索是@android:id)。

setListAdapter(new ArrayAdapter<String>(this, R.layout.tasks_list_row, android.R.id.text1, items_task));

除非您使用內置布局(例如android.R.layout.two_line_list_item),否則這樣做沒有太大優勢。

嘗試使用android默認的textview ID; android.R.id.text1

為您的LinearLayout android:id =“ @ android:id / tasks_list_row”指定一個ID,並改用它

setListAdapter(new ArrayAdapter<String>(this, R.id.tasks_list_row, items_task));

每個項目都將具有您指定的xml。 您可以改用CheckedTextView,它結合了TextViewCheckBox

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM