我有一個頁面,返回從數據庫中退回的項目列表。我想用onClick動態地將每個項目添加到我的android片段中,作爲一個複選框,它可以指示某個項目是否被選中或取消選中。動態列表複選框與onclicks
如何通過點擊和每個不同的標題動態添加複選框?
下面是我插入複選框到XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#e5e5e5"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="@+id/styleDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"
></TextView>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="@+id/checkBox" />
</LinearLayout >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="horizontal"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<Button
android:id="@+id/buttonAddList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create List"
style="?android:attr/borderlessButtonStyle"
android:textColor="@color/orange"
android:textStyle="bold"
/>
</LinearLayout >
</FrameLayout>
</LinearLayout>
</ScrollView>
我目前在上面的代碼中一個複選框。我打算刪除這個。該複選框只是爲了顯示我希望我的複選框顯示的位置。
只要在哪裏扔onclick監聽器?在我的異步任務結束? – Mike
我不知道該如何回答;也許我能夠看到更多的代碼,但我不能承諾任何事情。 這裏的問題究竟是什麼?您在創建CheckBox對象實例時能不能設置偵聽器,就像我提供的示例中那樣? – JakeP