【问题标题】:Chechbox listView with adapter with checkbox "select All" [duplicate]带有适配器的复选框列表视图,带有复选框“全选”[重复]
【发布时间】:2014-04-11 08:45:37
【问题描述】:

我有一个 ListView 的 ArrayAdapter 包含一个复选框项目列表 当我检查第一个项目时,我想检查我的 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;
    }
}

【问题讨论】:

    标签: android listview checkbox android-arrayadapter


    【解决方案1】:
    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2018-02-14
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      相关资源
      最近更新 更多