uncheck all checbox in listview in android

☆樱花仙子☆ 提交于 2020-01-06 07:46:27

问题


i want to uncheckall checkbox in customlistview.my adapter works fine so on click button

this is written in my button listener

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

                     }

in toggle method

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

Adapter

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

the problem with this code in view i get 6 child when i scroll down i get again 6 child ..the childs are the item in listview when scrolling up or down which is shown in view so the items of listview shown is child of listview..so i want all child to uncheck but with this code it is not working please tell me how can i do it?


回答1:


This is a simple way to uncheck all the children CheckBoxes of a ViewGroup (ListView extends ViewGroup). You'll just want to pass your ListView into this method.

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



回答2:


One problem is where you do:

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

Your trying to get the checkbox associated with "v", but findViewById() doesn't do that. Instead your just creating a NEW checkbox from your XML.

I think what you want to do is modify the underlying data "dcates" and then call notifyDataSetChanged() on your adapter.

Something like:

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

listview.getAdapter().notifyDataSetChanged();



回答3:


In your adapter (customAdapter) include a method call 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();
}

In your getView(). according to state of variable(isSelected), select or deselect the checkbox.

Hope this helps.



来源:https://stackoverflow.com/questions/14509552/uncheck-all-checbox-in-listview-in-android

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