Select All items of a ListView (custom row with checkbox in it)

守給你的承諾、 提交于 2019-12-03 10:46:06

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.

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}

ingsaurabh

have you looked this Correct way to check all checkboxes in ListView?

int count = getListAdapter().getCount();

I think you should run this long-running task off the UI thread. When you click button in OnClickListener:

new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < list.getAdapter().getCount(); i++) {
                                final int position = i;
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        list.setItemChecked(pos, true);  
                                    }
                                });
                            }
                        }
                    }).start();     

and in onCreate() :

this.mHandler = new Handler();

Each item in list view should be Checkable like CheckableRelativeLayout that implements Checkable interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!