2013-05-09 61 views
0

我有一個列表視圖,其中有一個TextView和一個複選框。 ListView用於填充本地SQLite數據庫中的數據。如果點擊按鈕,如何獲得ListView子項的id,其複選框被打勾?ListView中的複選框

以下是我的main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="top" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ListView 
android:id="@+id/list_data" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_alignParentRight="true" 
android:layout_below="@+id/textViewTitle" 
android:layout_marginTop="26dp" 
android:layout_weight="1" 
android:dividerHeight="0dip" > 
</ListView> 
<TextView 
android:id="@+id/textViewTitle" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:text="Things To Remember" 
android:textSize="25sp" /> 


</RelativeLayout> 

我subitem.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentLeft="true" 
android:layout_alignParentTop="true" 
android:text="Title:" 
android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
android:id="@+id/tv_title" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_alignBaseline="@+id/textView1" 
android:layout_alignBottom="@+id/textView1" 
android:layout_alignRight="@+id/check" 
android:layout_marginLeft="44dp" 
android:layout_marginRight="27dp" 
android:layout_toRightOf="@+id/textView1" 
android:paddingBottom="5dp" 
android:text="Test Data" 
android:textSize="15sp" /> 

<CheckBox android:id="@+id/check" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:layout_marginLeft="4px" 
android:layout_marginRight="10px" android:layout_alignParentRight="true" 
android:focusable="false" 
></CheckBox> 



</RelativeLayout> 

我的問題是,如果我檢查的複選框中的每個子項,然後按菜單按鈕,怎樣才能使每個選定的子項目的id變得可變。

回答

0

您使用

SparseBooleanArray checks = yourlistView.getCheckedItemPositions(); 

讓所有檢查位置,那麼你就可以得到IDS與

private final ArrayList<Long> getCheckedIds() { 
    ArrayList<Long> ids = new ArrayList<Long>(); 
    SparseBooleanArray checks = yourlisView.getCheckedItemPositions(); 
    if (checks == null) { 
     return ids; 
    } 
    for (int i = 0, count = checks.size(); i < count; i++) { 
     int key = checks.keyAt(i); 
     long id = yourlistview.getAdatper().getItemId(key); 
     if (checks.get(key) && id >= 0) { 
      ids.add(id); 
     } 
    } 
    return ids; 
} 
+0

感謝您的代碼。 但是,當我編寫代碼的第二部分時,它說創建一個方法「getCheckedIds()」。 [IMG] http://i43.tinypic.com/9jhb3s.png [/ IMG] [IMG] http://i41.tinypic.com/232pds.png [/ IMG] – 2013-05-09 02:50:33

+0

你是否在另一種方法? – buptcoder 2013-05-09 03:02:03

+0

我可以看到你在'onCreate'方法中寫入方法... – buptcoder 2013-05-09 03:08:27