2011-08-14 49 views
1

我已經花了整整一天的時間嘗試獲取自定義行加載。在StackOverflow和其他地方,似乎有很多關於如何將複選框綁定到Android列表視圖中的一行數據的示例,但它們似乎都不完整。帶選項的Android ListView

這裏是我有(row.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="40dip" 
    android:orientation="horizontal" android:background="@drawable/row_bk" 
    android:layout_gravity="center_vertical"> 

    <TextView android:id="@+id/ItemID" 
    android:layout_weight="0" android:visibility="gone" 
     android:layout_width="0dp" android:gravity="center" 
    android:layout_height="wrap_content" /> 

<TextView android:id="@+id/Title" 
    android:layout_width="0dp" android:textColor="#666" 
    android:layout_weight="1" android:layout_height="40dip" 
    android:padding="5dp" android:gravity="center_vertical" 
    android:textSize="17dip" /> 

<CheckBox android:id="@+id/chkCheck" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="false" android:clickable="true"></CheckBox> 

</LinearLayout> 

在代碼中,我有一個SimpleCursorAdapter,填充的ListView。

Cursor oLoop = db 
      .rawQuery(
      "select _id, title, is_checked from tbl",null); 

    startManagingCursor(oLoop); 

    String[] from = new String[] { "_id", "title", "is_checked" }; 

    int[] to = new int[] { R.id.ItemID, R.id.Title, R.id.chkCheck }; 

    SimpleCursorAdapter oList = 
    new SimpleCursorAdapter (this, R.layout.task_item_row, oLoop, from, 
    to); 

    setListAdapter(oList); 

過去,我不太確定該怎麼做。如果有人能指出我一個很好的例子,那會很棒。目標是實際上能夠切換複選框。

在此先感謝! 亞歷

回答

1

亞歷克斯,

下一步是實現ViewBinder.setViewValue()

這將是這個樣子:

getViewAdapter().setViewBinder(
    new ViewBinder(){ 
     public boolean setViewAdapter(View view, Cursor cursor, int columnIndex){ 
      <JavaType> object = cursor.get<JavaType>(columnIndex); 

      boolean isHandled = false; 
      if(view.getId() == R.id.checkBox){ 
       CheckBox cb = (CheckBox) view; 
       cb.setChecked(isObjectChecked(object)); 
       // object depends on your underlying data type 
       // in the data base, use the debugger to find the actually implemented type. 
       isHandled = true; 
      } 

      return isHandled; 
     } 
    } 
); 

這可以是有條件可見視圖和加載一個非常強大的方法Uri的網絡等等。

+0

是的,它做到了!這是我失蹤的一塊!當然,也不是一個明顯的步驟。我必須爲後人記錄這個地方。 – LTMOD

+0

@LTMOD它在[SimpleAdapter](http://developer.android.com/reference/android/widget/SimpleAdapter.html)文檔中。 –