【发布时间】:2011-01-29 09:34:40
【问题描述】:
我有N“书”,每本书都有一些章节。所以我创建了一个ListView。 ListView 中的每个项目都有两部分:TextView 用于名称,Spinner 用于列出章节。
我为ListView - BookAdapt 创建了一个自定义适配器,并为Spinner - ChapAdapt 创建了一个自定义适配器。我有它显示和工作,但我不知道如何添加点击/选定事件。
这里是列表资源
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Chapter" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:text="Load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:text="Cancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<ListView
android:id="@+id/chapview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
这是每本书的展示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/viewbook"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Spinner
android:id="@+id/chapspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/chapter_prompt"
android:layout_weight="1"/>
这里是来自BookAdapt的关键部分
class BookAdapt extends ArrayAdapter {
BookAdapt( Context context, int txtres, BookHolder[] bks) {
super( context, txtres );
mContext = context;
books = bks;
mInflater = LayoutInflater.from(context);
}
public View getView( int pos, View convert, ViewGroup parent) {
BookHolder book = books[ pos];
String bkname = book.getName();
if (convert == null) {
convert = mInflater.inflate(R.layout.bookview, null);
book.text = (TextView) convert.findViewById(R.id.viewbook);
Spinner sp = (Spinner)convert.findViewById( R.id.chapspinner);
ChapAdapt ch = new ChapAdapt( book, mContext);
sp.setAdapter( ch);
//convert.setClickable(true);
convert.setTag( book);
} else {
book = (BookHolder)convert.getTag();
}
book.text.setText( bkname );
return convert;
}
ChapAdapt 只需分配一个TexTView。
【问题讨论】:
标签: android android-layout onclick spinner android-listview