选中所有复选框

时间:2013-01-16 08:44:10

标签: android listview checkbox

我想实施一个场景 - 屏幕顶部有一个复选框,并且有一个自定义列表视图,其顶部复选框下方有复选框。

我希望有人检查该复选框应检查所有列表视图复选框,反之亦然。

public class GroupMemberListAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;
        Context Mycontext;

        public GroupMemberListAdapter(Context context) {
            Mycontext = context;
            inflater = LayoutInflater.from(context);

        }

        public int getCount() {
            return broadcastList.size();

        }

        public Object getItem(int paramInt) {
            return paramInt;
        }

        public long getItemId(int paramInt) {
            return paramInt;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            EventViewHolder holder;

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.contactcustomlayout,
                        null);
                holder = new EventViewHolder();

                holder.mtvGroupMemberName = (TextView) convertView
                .findViewById(R.id.tvGroupMemberName);

                holder.checkbox_group_member = (CheckBox)convertView.findViewById(R.id.checkBox_GroupMember);

                /*holder.mtvGroupMemberAbout = (TextView) convertView
                        .findViewById(R.id.tvGroupMemberAbout);*/

                convertView.setTag(holder);
            } else {
                holder = (EventViewHolder) convertView.getTag();
            }
            holder.mtvGroupMemberName.setText(""+broadcastList.get(position));
            //holder.mtvGroupMemberAbout.setText(""+data.get(position));
            return convertView;
        }

        public class EventViewHolder {
            private TextView mtvGroupMemberName;
            private TextView mtvGroupMemberAbout;
            private CheckBox checkbox_group_member;
        }
    } 


checkBox_selectAll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                checkAllBoxes();

            }
        });


    private void checkAllBoxes(){

        int size =  mlvGroupBroadContact.getAdapter().getCount();
        System.out.println("Size of the list is:"+size);


    }

如何查看所有列表视图框

3 个答案:

答案 0 :(得分:1)

如果您的ListView选择模式为CHOICE_MODE_MULTIPLECHOICE_MODE_SINGLE

    private void deselectAll() {

        getListView().clearChoices(); 

       //--as of GingerBread, clearChoices() alone is not sufficient--
       //--we'd have to manually un-check visible items--

        for (int i = 0; i < getListView().getChildCount(); i++) {
            View c = getListView().getChildAt(i);
            if (c instanceof Checkable) {
                ((Checkable) c).setChecked(false);
            }
        }

    }

    private void selectAll() {

        SparseBooleanArray sba = getListView().getCheckedItemPositions();

        for (int i = 0; i < getAdapter().getCount(); i++) {
            sba.put(i, true);
        }

        for (int i = 0; i < getListView().getChildCount(); i++) {
            View c = getListView().getChildAt(i);
            if (c instanceof Checkable) {
                ((Checkable) c).setChecked(true);
            }
        }

    }

getListView()getAdapter()替换为您的ListView及其Adapter个实例。

答案 1 :(得分:1)

您可以使用以下代码检查列表视图中的所有复选框

private void checkAllBoxes(){

    int size =  mlvGroupBroadContact.getAdapter().getCount();
    System.out.println("Size of the list is:"+size);
    CheckBox chk;
    for(int i=0;i<size;i++)
    {
      chk= ((CheckBox)jobList.getChildAt(i).findViewById(R.id.chk));
      chk.setChecked(true);
    }
}

答案 2 :(得分:0)

从checkBox_selectAll点击列表器中更新适配器类中的布尔变量。 并调用view.getAdapter.notifyDataSetChanged();

在基于此变量的getView中设置状态已选中或未选中。