带有适配器的复选框listView带有复选框“全选”

时间:2014-03-08 00:03:04

标签: android listview checkbox android-arrayadapter

我有一个ListView的ArrayAdapter包含一个复选框项目列表 我想检查第一项

时检查我的listView的所有项目

我尝试在listview的OnItemClickListener中这样做

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

1 个答案:

答案 0 :(得分:0)

@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