如何在ListView中添加复选框?

4
我有一个问题,卡了一段时间,我不知道如何在列表中添加复选框。例如,如果我有一系列项目,我希望能够勾选它们。我的XML代码如下:
<LinearLayout android:id="@+id/topLayout"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_centerHorizontal="true"  android:layout_height="wrap_content">

</LinearLayout>

<LinearLayout
    android:id="@+id/middleLayout"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <LinearLayout android:id="@+id/leftMiddleLayout"
            android:orientation="vertical" android:layout_below="@+id/topLayout"
            android:layout_above="@+id/bottomLayout"
            android:layout_width="60px" android:layout_height="wrap_content"
            >

            <ListView android:id="@+id/checkboxList" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" ></ListView>

            <CheckBox android:id="@+id/checkbox"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:checked="false"
            android:text="test">
            </CheckBox>

    </LinearLayout>

    <LinearLayout android:id="@+id/rightMiddleLayout"
            android:orientation="vertical" android:layout_below="@+id/topLayout"
            android:layout_above="@+id/bottomLayout"
            android:layout_width="280px" android:layout_height="wrap_content"
            >

            <ListView android:id="@+id/list" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" ></ListView>

            <TextView android:id="@+id/text" android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>                   
    </LinearLayout>
</LinearLayout>

<LinearLayout android:id="@+id/bottomLayout" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:paddingBottom="5pt"
    >

    <EditText android:id="@+id/insertNewItem"
        android:layout_width="220px" android:layout_height="wrap_content" />

    <TextView android:layout_width="10px" android:layout_height="wrap_content" />

    <Button android:id="@+id/addItemButton" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:text="Add Item"/>
</LinearLayout>

如果您有任何想法,请告诉我,这是为了我的学术研究:(( 谢谢!

无法解决,看起来像是一个无法解决的问题。天哪,我显然很蠢。 - Bugzy bug
5个回答

5
final ListView lView = (ListView) findViewById(R.id.ListView01);
lView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

这里的 item 是一个数组。


4
public class myAdapter extends SimpleCursorAdapter {
    public myAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout, cursor, from, to);  
}

@Override   
public void bindView(View view, Context context, Cursor cursor) {
    cb=(CheckBox)view.findViewById(R.id.cb);
    cb.setText(dbgetStringValue);
    cbText=(TextView)view.findViewById(R.id.cbText);
            cbText.setText(dbgetStringValue);
    cb.setChecked(false);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton cb, boolean isChecked) {            
            if(cb.isChecked()) {                    
                // action
            }
            else if(isChecked==false) {
                // action
            }
        }           
    });
}
} 

这是您想要的吗?


3
我建议您观看来自 Android 开发者网站的视频,了解如何 使您的 Android UI 快速高效。它将为您提供代码以解决问题,并展示适配器实现的正确方式,确保其尽可能快速。

0
如果您在listView上使用ListAdapter(例如自定义的BaseAdapter)并使用setAdapter(),则可以在adapter的getView()中使用LayoutInflater提供任何喜欢的列表项布局,包括CheckBox。

哎呀,我解决不了这个问题...我被卡住好久了 :((( - Bugzy bug

0
或者您可以扩展任何ListAdapter到子类并覆盖bindView。在if语句内,您可以为ChecBoxes设置.setText!

哦,谢谢! 你知道在哪里可以找到这方面的例子吗? 问题是我正在使用JSON从数据库读取项目,所以我有项目。我为此设置了一个ListAdapter,我可以看到项目。问题是我找不到一种方法在每个项目旁边放置复选框:( - Bugzy bug

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接