Checkbox在listview android中取消选中?

时间:2014-01-23 09:18:56

标签: android listview checkbox

我知道有很多解决方案可以解决这个问题,但我不是一个正确的解决方案,当选中一个复选框时,我正在为它分配一个ID,并将其添加到一个数组中,但是当向下滚动时,取消选中该复选框,但ID存储在数组中,如果选中该复选框,我希望保持复选框。

我的适配器类是。

public class sendivitesadapter extends ArrayAdapter<Item> implements Filterable,SectionIndexer{
    private Context context;
    private ArrayList<Item> items;
    private qrusers qrusers;
    private LayoutInflater vi;
    private String[] udis;
    private final boolean[] mCheckedState;
    qrusers qrus;

    private ItemsFilter mFilter;

    public sendivitesadapter(Context context,ArrayList<Item> items,String[]udis) {
        super(context, 0,items);

        this.context = context;
        mCheckedState = new boolean[items.size()];
        this.qrusers =(qrusers) context;
        this.items = items;
        this.udis=uid;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Item getItem(int position) {
        return super.getItem(position);
    }

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

        View v = convertView;

        final Item i = items.get(position);

        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());

            }else{
                sendItem ei = (sendItem)i;
                v = vi.inflate(R.layout.checkboxlist, null);
                final TextView title = (TextView)v.findViewById(R.id.contactname);
                final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
                 checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
                //checkBox.setTag(position);
                //items.addAll(uid);

               checkBox.setTag(udis[position]);

               checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        s = (String)buttonView.getTag();
                        Log.e("IDDDDDDDD", s);

                        userid.add(s);
                        Log.e("Array", userid.toString());
                    } else {
                        s = (String)buttonView.getTag();
                        userid.remove(s);

                    }
                    SharedPreferences app_preferences = PreferenceManager
                            .getDefaultSharedPreferences(qrusers.this);
                    SharedPreferences.Editor editor = app_preferences.edit();

                    editor.putString("userid", TextUtils.join(",", userid));
                    editor.commit();
                }
            });


                //    Log.e("IDDDDDDD", text);
                    //checkBox.setTag("12");
                /*  checkBox.setOnClickListener(new OnClickListener() {


                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                        if(checkBox.isChecked()){
                             s= (String)v.getTag();
                            Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
                        userid.add(s);

                        Log.e("Array", userid.toString());

                        }
                        else{
                        s=(String)v.getTag();
                        userid.remove(s);
                        }

                        }
                    });*/
                    if (title != null) 
                        title.setText(ei.contactname);
                    if(subtitle != null)
                        subtitle.setText(ei.companyname);

                }
            }
            return v;
        }

2 个答案:

答案 0 :(得分:1)

如果要存储复选框的状态,则必须在内部ListView或您正在使用的任何结构以及扩展适配器中似乎称为items的结构中更新它,并且之后忘记打电话给notifyDataSetChanged()

答案 1 :(得分:0)

这个问题有你的答案。您必须维护一个单独的结构来检查/取消选中复选框。

ListView Viewholder checkbox state