2014-03-26 49 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()來代替。