选择在Android的ListView中的所有复选框 [英] select all check box in android listview

查看:183
本文介绍了选择在Android的ListView中的所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我试图创建多个复选框列表。

In my Application, i tried to create a list with multiple check boxes.

对于我试过一次的ListView android.R.layout.simple_list_item_multiple_choice 多选在的ListView

for that i tried one ListView with android.R.layout.simple_list_item_multiple_choice for multiple choice in ListView.

我在XML中的ListView的,..

My ListView in XML as,..

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="468dp"
        android:choiceMode="multipleChoice"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" >

和我使用复选框来选择/取消选择所有的复选框的ListView

And i use an CheckBox to select/deselect all the CheckBox in the ListView as

   <CheckBox
            android:id="@+id/select_all"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:textSize="18dp"
            android:text="Select all" />

和java code作为,..

and java code as,..

selectall.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            int size = 0;
            boolean isChecked = selectall.isChecked();
            if (isChecked == true) {
                size = getListView().getCount();
                for (int i = 0; i <= size; i++)
                    l1.setItemChecked(i, true);
            } else if(isChecked==false)
            {
                size = getListView().getCount();
                for (int i = 0; i <= size; i++)
                    l1.setItemChecked(i, false);
            }
        }
    });

下面我分配了L1作为我的的ListView 。现在我的全部选中复选框的工作非常好选择/取消所有复选框中的的ListView

Here i have assigned the l1 as my ListView. Now my Select All check box working very well for Select/Deselect all the check boxes in the ListView.

但是,如果我点击全选复选框,它不是取消勾选..后取消的项目之一

But, if i deselect one of the item after clicking select all check box, its not un-checking..

如果我在列表中手动意味着选择所有的复选框,选择所有的复选框需要自动检查..

And if i select all the check boxes in the list manually means, the Select all CheckBox needs to checked automatically..

我用下面的codeS为ListAdapter

I used the following codes for ListAdapter

ArrayAdapter<String> adapter1 = new ArrayAdapter<String>
       (this,Android.R.layout.simple_list_item_multiple_choice,list);
     setListAdapter(adapter1);
     l1=getListView();

我想实现这个解决方案。

I'm trying to achieve this Solution..

推荐答案

您可以使用HashMap或一个List theObjectRow 以跟踪哪些sheckbox检查用布尔的。

you could use a HashMap or a List of theObjectRow to keep track of which sheckbox is checked with a boolean.

然后当你点击 checkAll 您更新您的列表中真正的全布尔做yourAdapter.notifyDataSetChanged();

then when you click on checkAll you update all boolean in your list to true and do yourAdapter.notifyDataSetChanged();

当然,在你的适配器(baseadapter例如),您使用的HashMap /列表中GetView给init细胞。

of course in your adapter (baseadapter for example) you use the HashMap/List in GetView to init the cells.

if(list.get(position).isChecked()){
    cellHolder.checkBox.setChecked(true);
}
else{
    cellHolder.checkBox.setChecked(false);
}

所有的布尔处于真正让所有的复选框会检查(同样的逻辑也适用于取消)

All the boolean are at true so all the checkBox ll be checked (the same logic goes for uncheck)

希望它帮助 好运气

这篇关于选择在Android的ListView中的所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆