在自定义列表视图复选框问题 [英] Checkbox issue in custom listview

查看:203
本文介绍了在自定义列表视图复选框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要选择所有的复选框自定义的ListView 单按钮的点击。

但是,当我在ListView中超过9个项目我得到的NullPointerException下code的第二道防线。

 查看VI = diffeneceLv.getChildAt(I)
复选框CB =(复选框)vi.findViewById(R.id.conschkbx);


解决方案

您也越来越理解错了,的ListView 重新使用您的行,这意味着创建的行数/在内存布局不等于阵列您的项目。

通常的ListView 在滚动。重新设置新的数据,以previous行

我建议你学习<一href=\"http://vikaskanani.word$p$pss.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/\"相对=nofollow>这个博客帖子,这里笔者维修器材的选中状态,然后在 getView()。

作者创造bolean数组是这样的:

 私人布尔[] thumbnailsselection;

和存储检查的状态或取消选中,后来从 getView访问它(),你会做什么,你将存储所有指数,并刷新您的适配器。它会选择所有行。

下面是另一篇文章

I want to select all CheckBox from Custom ListView on single Button click .

But when I have more than 9 items in ListView I am getting NullPointerException in below second line of code.

View vi= diffeneceLv.getChildAt(i);
CheckBox cb = (CheckBox) vi.findViewById(R.id.conschkbx);

解决方案

You are getting it wrong, ListView re-uses your rows, which means number of created rows/layouts in memory are not equal to your items in array.

Typically ListView re-sets the new data to previous row upon scroll.

I would suggest you to study this blog post, here the author is maintaing the Checked state and then setting it accordingly in getView() of adapter.

The author have created an array of bolean like this:

private boolean[] thumbnailsselection;

and storing the state of check or uncheck, and later accessing it from getView(), what you will do is, you will store true for all index and refresh your adapter. It'll select all your rows.

Here is another post.

这篇关于在自定义列表视图复选框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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