【问题标题】:how to add a checkbox in a listview?如何在列表视图中添加复选框?
【发布时间】:2010-03-29 14:17:01
【问题描述】:

我有一个问题,卡了一段时间, 我不知道如何在列表中添加复选框,例如,如果我有一个项目列表,我希望能够检查它们。我的 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>

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

谢谢!

【问题讨论】:

  • 无法解决,似乎是一个无法解决的问题。我的天啊。我显然很愚蠢

标签: android listview checkbox


【解决方案1】:
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 是一个数组。

【讨论】:

    【解决方案2】:
    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 开发网站的有关如何Make your Android UI Fast and Efficient 的视频。它将为您提供解决问题的代码,并向您展示实现适配器的正确方法,以确保它尽可能快。

      【讨论】:

        【解决方案4】:

        如果您通过 setAdapter() 在 listView 上使用 ListAdapter(例如自定义 BaseAdapter),您可以在适配器的 getView() 中使用 LayoutInflater 为列表条目提供您喜欢的任何布局。这可以包括一个复选框。

        【讨论】:

        • 天啊,我无法解决这个问题......我已经坚持了很久了:(((
        【解决方案5】:

        或者您可以将任何 ListAdapter 扩展为子类并覆盖 bindView。如果您要为 ChecBoxes 设置 .setText,请在里面!

        【讨论】:

        • 哦,谢谢!你知道在哪里可以找到这样的例子吗?问题是我正在使用 JSON 从数据库中读取项目,所以我有项目。我为此设置了一个 ListAdapter,我可以看到这些项目。问题是我找不到在每个项目旁边放置一个复选框的方法:(
        猜你喜欢
        • 1970-01-01
        • 2014-10-29
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 2011-11-15
        • 2017-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多