取消选中android中listview中的所有复选框

时间:2013-01-24 19:51:54

标签: android listview checkbox uncheck

我想在customlistview.my适配器中取消选中复选框正常工作,点击按钮

这是在我的按钮监听器中写的

 for(int i = 0; i<listview.getChildCount();i++)
                    {
                     v = listview.getChildAt(i);
                     CheckBox cv =(CheckBox)v.findViewById(R.id.checktitle);
                     if(cv.isChecked())
                     {
                         // cv.setChecked(false);
                         //listview.setItemChecked(i, false);
                           toggle(cv);

                     }
切换方法

 public void toggle(CheckBox v)
         {
                 if (v.isChecked())
                 {
                     v.setChecked(false);
                 }
                 else
                 {
                     v.setChecked(true);
                 }
         }

适配器

public class customAdapter extends ArrayAdapter {
    View view=null;
    Context context;
    ViewHolder holder;    boolean checkAll_flag = false;
    boolean checkItem_flag = false;
    List<CustomDishMenus> dcates=new ArrayList<CustomDishMenus>();
    public customAdapter(Context context, int textViewResourceId, List objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
        this.context=context;
        this.dcates=objects;
    }
     static class ViewHolder {
            protected TextView text;
            protected CheckBox checkbox;
        }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         holder = new ViewHolder();
            final CustomDishMenus ords=dcates.get(position);
                LayoutInflater layoutInflater=(LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
                convertView=layoutInflater.inflate(R.layout.tablayout,parent, false);
                if(convertView!=null){

                    holder.text = (TextView) convertView.findViewById(R.id.title);
                    holder.checkbox = (CheckBox) convertView.findViewById(R.id.checktitle);

                    holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub
                              int getPosition = (Integer) buttonView.getTag();  
                                dcates.get(getPosition).setSelected(buttonView.isChecked());

                        }
                    });
                     convertView.setTag(holder);
                        convertView.setTag(R.id.title, holder.text);
                        convertView.setTag(R.id.checktitle, holder.checkbox);
                        } else {
                        holder = (ViewHolder) convertView.getTag();

                }
                  holder.checkbox.setTag(position); // This line is important.

                    holder.text.setText(dcates.get(position).getDishName());
                    holder.checkbox.setChecked(dcates.get(position).isSelected());

                return convertView;
    }

这个代码在视图中的问题当我向下滚动时我得到6个孩子我再次得到6个孩子..当在视图中显示向上或向下滚动时,孩子是列表视图中的项目因此显示的列表视图的项目是孩子列表视图..所以我希望所有孩子都取消选中,但是这段代码无法正常工作请告诉我该怎么做?

3 个答案:

答案 0 :(得分:11)

这是取消选中ViewGroup的所有子CheckBox的简单方法(ListView扩展ViewGroup)。您只想将ListView传递给此方法。

private void uncheckAllChildrenCascade(ViewGroup vg) {
    for (int i = 0; i < vg.getChildCount(); i++) {
        View v = vg.getChildAt(i);
        if (v instanceof CheckBox) {
            ((CheckBox) v).setChecked(false);
        } else if (v instanceof ViewGroup) {
            uncheckAllChildrenCascade((ViewGroup) v);
        }
    }
}

答案 1 :(得分:0)

您遇到的一个问题是:

CheckBox cv =(CheckBox)v.findViewById(R.id.checktitle);

您尝试将复选框与“v”相关联,但findViewById()不会这样做。而只需从XML创建一个新的复选框。

我认为您要做的是修改基础数据“dcates”,然后在适配器上调用notifyDataSetChanged()

类似的东西:

for (CustomDishMenus menu : dcates)
    menu.setSelected(false);

listview.getAdapter().notifyDataSetChanged();

答案 2 :(得分:0)

在适配器(customAdapter)中包含方法调用setAllSelected();

setAllSelected () {
    for (int i = 0; i < this.getCount(); i++) {
        AdapterItem info = this.getItem(i);

        // info (add a boolean in you adapter item to store the state of checkbox i.e. isSelected and mark it to true. )
    }
    notifyDataSetChanged();
}

getView()中。根据变量状态(isSelected),选择或取消选中复选框。

希望这有帮助。

相关问题