选择一个ListView中的所有项目(自定义使用复选框中它排) [英] Select All items of a ListView (custom row with checkbox in it)

查看:182
本文介绍了选择一个ListView中的所有项目(自定义使用复选框中它排)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么:我有一个自定义行一个ListView,有一个复选框和放大器; 2 TextViews每一行中。我有全选按钮。

What I have: I have a ListView with custom rows, having a CheckBox & two TextViews in each row. I have a button for "Select All".

我要什么:我想,当我按一下按钮,在ListView中所有的复选框得到选中/取消选中

What i want: I want that when I click the button, all the CheckBox in ListView get checked/unchecked.

什么问题:在的全选按钮的OnClick。我是这样做的:

What is the problem: In OnClick of the "Select All" button. i am doing this:

public void OnClickSelectAllButton(View view)
{
    ListView l = getListView();
    int count = l.getCount();
    for(int i=0; i<count; ++i) 
    {
       ViewGroup row = (ViewGroup)l.getChildAt(i);
       CheckBox check = (CheckBox) row.findViewById(R.id.checkBoxID);
       check.setChecked(true); // true for select all and false for unselect all etc..
    }
}

下面的 l.getChildAt(我)是给我唯一可见的项目。当指数超出可见项,将出现问题。我要检查所有的复选框列表,而不仅仅是可见的。

Here l.getChildAt(i) is giving me the visible items only. And when the index goes out of visible items, the problem occurs. I want to check all the CheckBox in List, not just the visible ones.

推荐答案

这会发生,因为ListView控件适配器重用的意见。你正在尝试做的方式是不正确。我不认为你永远应该通过列表视图的孩子访问列表视图行。

It will occur, because ListView adapter reuses views. The way you are trying to do is incorrect. I don't think you ever should access listview rows through listview children.

在您的活动引入一个变量,将保持当前的状态(布尔checkAll )。当用户presses按钮,就必须将checkAll为true,并调用 notifyDataSetChanged()(对于arrayadapter),或重新查询()(为的CursorAdapter)在您的ListView的适配器。在适配器的 getView()方法引入检查这个标志,所以如果(checkAll){选中该复选框}

Introduce a variable in your activity, that will hold the current state (boolean checkAll). When the user presses the button, it must set "checkAll" to true, and call notifyDataSetChanged() (for arrayadapter), or requery() (for cursoradapter) on your ListView's adapter. In adapter's getView() method introduce a check for this flag, so if (checkAll) {check the check box}

这篇关于选择一个ListView中的所有项目(自定义使用复选框中它排)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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