我有一个页面,返回从数据库中退回的项目列表。我想用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