2011-03-02 74 views
0

我的LinearLayout:帶有複雜項目的ListView,如何知道項目的狀態?

<LinearLayout 
    xmlns:android= "http://schemas.android.com/apk/res/android" 
    android:layout_width= "fill_parent" 
    android:layout_height= "fill_parent"> 
<TextView android:id="@+id/DictLink" android:layout_width="wrap_content" android:layout_height="wrap_content" ></TextView> 
<CheckBox android:text="" android:id= "@+id/DictTitle" android:layout_width="fill_parent" android:layout_height="wrap_content" ></CheckBox> 
</LinearLayout> 

它用作ListView項。下面的代碼爲ListView創建一個數組適配器。

ArrayList< HashMap< String, String >> adapter_list = new ArrayList< HashMap< String, String > >(); 

    for (String dict : dict_list) 
    { 
     String [ ] data = dict.split(";"); 
     HashMap< String, String > adapter_entry = new HashMap< String, String >(); 
     adapter_entry.put(FIELD_TITLE, data[ 0 ]); 
     adapter_entry.put(FIELD_LINK, data[ 2 ]); 
     adapter_list.add(adapter_entry); 
    } 

    String [ ] from = 
    { FIELD_TITLE, FIELD_LINK }; 
    int [ ] to = 
    { R.id.DictTitle, R.id.DictLink}; 

    final ListAdapter adapter = new SimpleAdapter(this, adapter_list, R.layout.dict_list_item , from, to); 

我怎麼知道哪些複選框被選中,哪些不被選中?

回答

0

ListView類具有的功能,這將有助於你:

int ListView.getCheckedItemPosition()(單項選擇列表) SparseBooleanArray ListView.getCheckedItemPositions()(多選列表)

多: http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemIds()

+0

如果我設置了list_view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); SparseBooleanArray返回給我12個錯誤,但我檢查了2個項目。可能我需要連接ListView與LinearLayout id/DictTitle上的複選框,但如何做到這一點? – Arkaha 2011-03-03 19:41:50

+0

確保選中該複選框還會觸發ListView項目上的performItemClicked,因爲此函數實際上是在內部更改已檢查的狀態。 – 2011-03-04 00:34:02

+0

http://tokudu.com/2010/android-checkable-linear-layout/ – 2011-03-21 22:09:21