Search code examples
androidlistviewcheckboxandroid-arrayadapter

Chechbox listView with adapter with checkbox "select All"


i have an ArrayAdapter for a ListView contains a list of checkboxes items and i want to check all items of my listView when i check the first item

i try to do it in the OnItemClickListener of the listview like this

loff.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    if(position == 0 )
                    {
                        System.out.println("check alllllllllllllllllllllllllllllllllllllllll");
                        CheckBox c = (CheckBox)view;
                        for(int i=0;i<categ.size();i++)
                        {
                            Categories cat = adoff.getItem(position);
                            CheckBox ch = (CheckBox)parent.getChildAt(i);
                            ch.setChecked(true);
                        }

                    }
                    else
                    {
                        Log.e("pfffffff", "pffffffffffffffffffffffffffffffffffffffff");
                    }
                }
            });`but it doesn't work

:(

`

    public class AdapterOffLine extends ArrayAdapter<Categories> {                 public ArrayList<Categories> categ;
    Context c;
    public AdapterOffLine(Context context, int textViewResourceId,ArrayList<Categories> categ) {
        super(context, textViewResourceId, categ);
        this.categ = new ArrayList<Categories>();
        this.categ.addAll(categ);
        c = context;
        System.out.println(categ.size()+" dans le AdapterOffLine");
    }

    private class ViewHolder {
        CheckBox choix;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.offlineitem, null);

            holder = new ViewHolder();
            holder.choix = (CheckBox) convertView.findViewById(R.id.choix);
            convertView.setTag(holder);
            holder.choix.setText(categ.get(position).getNom());
            holder.choix.setChecked(categ.get(position).isSelected());
            holder.choix.setTag(categ.get(position));


            holder.choix.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Categories m = (Categories) cb.getTag();

                    Toast.makeText(getContext(),"Checkbox: " + cb.getText() + " -> "
                                    + cb.isChecked(), Toast.LENGTH_LONG)
                            .show();

                    m.setSelected(cb.isChecked());
                }
            });

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }
}

Solution

  • @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub
                        if(position == 0 )
                        {
                            System.out.println("check alllllllllllllllllllllllllllllllllllllllll");
                            CheckBox c = (CheckBox)view;
                            for(int i=0;i<categ.size();i++)
                            {
                                Categories cat = adoff.getItem(position);
                                cat.setSelected(true);
                                ((AdapterOffLine) parent).notifyDataSetChanged();
                            }
    
                        }
                        else
                        {
                            Log.e("pfffffff", "pffffffffffffffffffffffffffffffffffffffff");
                        }
                    }
                });`but it doesn't work