2014-03-26 44 views
2

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

我目前在上面的代码中一个复选框。我打算删除这个。该复选框只是为了显示我希望我的复选框显示的位置。

回答

2

你需要做的第一件事就是向你的LinearLayout(在那个XML文件中)添加一个id,这个id是用来存放复选框的。然后,在代码中,您需要通过其ID来获取LinearLayout,并使用addView()添加动态创建的CheckBox。我想在伪代码它看起来像这样:

for (int i = 0; i < numberOfCheckBoxes; i++) { 

    CheckBox checkBox = new CheckBox(); 
    checkBox.setTitle("Your title"); 
    checkBox.setOnClickListener(new OnClickListener() { 

     // Your code to be executed on click 
    }); 

    linearLayout.addView(checkBox); 
} 

这有帮助吗?如果你保持代码清洁 - ADT(我相信Eclipse也是这样)给你Shift + Ctrl + F的快捷方式来自动缩进你的代码 - 尽可能经常使用它)

+0

只要在哪里扔onclick监听器?在我的异步任务结束? – Mike

+0

我不知道该如何回答;也许我能够看到更多的代码,但我不能承诺任何事情。 这里的问题究竟是什么?您在创建CheckBox对象实例时能不能设置侦听器,就像我提供的示例中那样? – JakeP

0

由于您正在处理数据库项目,我建议使用CursorAdapter为您完成繁重的工作。 CursorAdapter与任何Adapter类一样,可以处理数据库项并将它们自定义为适合您的布局,以便在ListView中使用。

你必须要调整你的代码:

  • 创建一个布局文件,其中包含要放置在动态列表不管。这是一个例子,说这是一个名为list_contents.xml:

    <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> 
    
  • 然后,而不是从你的AsyncTask返回一个列表,从您的数据库返回游标本身
    。这个游标将由CursorAdapter处理。我建议这个指南:
    http://www.gustekdev.com/2013/05/custom-cursoradapter-and-why-not-use.html

  • 实施CursorAdapter的方法:

在您的实现NewView的()的,膨胀list_contents.xml(请注意,如果您使用ResourceCursorAdapter你不会需要做到这一点)

在您的实现CursorAdapter.bindView的()做到这一点:

CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox); 
checkbox.setText(cursor.getString(cursor.getColumnIndex(YOUR_DATABASE_COLUMN_NAME_FOR_CHECKBOX_VALUES))); 
checkbox.setOnCheckedChangedListener(listenerInitializedSomewhereFromFragmentCode); 
  • 将您的ScrollView更改为ListView(它可以位于任何Layout中),并给它一个id,比如R.id.listview。

  • 最后,在您处理来自数据库,我们现在有一个指针,而不是列表中的一部分,只是这样做:

    CustomCursorAdapter CCA =新CustomCursorAdapter(getActivity(),resultFromDatabase,0); listView.setAdapter(cca);

注意:getActivity()适用于在Fragment中工作的情况。它应该是一个上下文,所以在一个Activity中它可以是“this”。

注意2:listView应该已经通过findViewById在此处初始化。

注意3:如果listView已经有一个Adapter和Cursor集合,你应该考虑调用listView.getAdapter()。changeCursor()来代替。