2011-03-02 67 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