0

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

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

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);


    }

如何检查所有列表视图框???

4

3 回答 3

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()and替换getAdapter()为您的ListView及其Adapter实例。

于 2013-01-16T08:50:57.443 回答
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);
    }
}
于 2013-01-16T08:51:15.397 回答
0

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

在基于此变量的 getView 中,将其状态设置为选中或未选中。

于 2013-01-16T09:06:44.643 回答