在Listview中丢失复选框的状态

时间:2014-05-09 14:08:35

标签: android android-listview android-checkbox

当我滚动listview时,我选择的checkbox's会互换

我的适配器类:

public class AdapterInvitePartner extends ArrayAdapter<FindPartnerPojo> {
    private LayoutInflater inflater;
    private List<FindPartnerPojo> objectList;
    private List<String> checkboxCheckedList= new ArrayList<String>();
    int i = 0;

    public AdapterInvitePartner(Context context, int textViewResourceId,
            List<FindPartnerPojo> objects) {
        super(context, R.layout.items_find_partners, objects);
        this.objectList = objects;
        this.inflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
         FindPartnerPojo item = objectList.get(position);
         final String id=item.getUserid();
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.items_find_partners, null);

            holder = new ViewHolder();

            holder.partner_email_addr_textview = (TextView) convertView
                    .findViewById(R.id.textView_partner_email_addr);
            holder.partner_phone_number_textview = (TextView) convertView
                    .findViewById(R.id.textView_partner_phone_number);
            holder.checkbox = (CheckBox) convertView
                    .findViewById(R.id.partner_checkBox);

            holder.checkbox
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            if (isChecked) {
                                checkboxCheckedList.add(id);
                            } else {
                                checkboxCheckedList.remove(id);
                            }

                        }

                    });

            convertView.setTag(holder);
            i++;
        }
        // if it's exist convertView then consume it
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.partner_email_addr_textview.setText(item.getEmail());
        holder.partner_phone_number_textview.setText(item.getPhone_number());

        return convertView;

    }

    static class ViewHolder {
        TextView partner_email_addr_textview, partner_phone_number_textview;
        CheckBox checkbox;
    }

    public List<String> getCheckBoxList() {
        return checkboxCheckedList;
    }

}

设置适配器:

adapter = new AdapterInvitePartner(getApplicationContext(),
                R.layout.items_find_partners, Constant.findpartnerlList);
        partner_listview.setAdapter(adapter);
        partner_listview.setCacheColorHint(0);

3 个答案:

答案 0 :(得分:0)

当转换convertView时,您必须手动重置复选框的值。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
         FindPartnerPojo item = objectList.get(position);
         final String id=item.getUserid();
        if (convertView == null) {

        convertView = inflater.inflate(R.layout.items_find_partners, null);

        holder = new ViewHolder();

        holder.partner_email_addr_textview = (TextView) convertView
                .findViewById(R.id.textView_partner_email_addr);
        holder.partner_phone_number_textview = (TextView) convertView
                .findViewById(R.id.textView_partner_phone_number);
        holder.checkbox = (CheckBox) convertView
                .findViewById(R.id.partner_checkBox);
        convertView.setTag(holder);
        i++;
    }
    // if it's exist convertView then consume it
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.partner_email_addr_textview.setText(item.getEmail());
    holder.partner_phone_number_textview.setText(item.getPhone_number());
        **holder.checkbox
                .setOnCheckedChangeListener(null);
        holder.checkbox.setChecked(the_original_value);**
        holder.checkbox
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        if (isChecked) {
                            checkboxCheckedList.add(id);
                        } else {
                            checkboxCheckedList.remove(id);
                        }

                    }

                });


    return convertView;

}

答案 1 :(得分:0)

由于视图回收,您必须设置可在getView中显式更改的所有属性。特别是,检查状态:

holder.checkbox.setChecked(checkboxCheckedList.contains(id));

如果之前已经检查过,请选中复选框。

答案 2 :(得分:0)

@Override

public int getViewTypeCount() {                 

    return getCount();
}

@Override
public int getItemViewType(int position) {

    return position;
}

将这两种方法添加到适配器中。从这里

https://stackoverflow.com/a/13643704/2042347