滚动时保留检查的Listview项目数

时间:2014-08-21 08:34:08

标签: android listview checkbox android-listview

我在Listview中有联系人列表,每个联系人都有checkBox。我想将检查的联系人数量限制为5.在此link后,我设法保留了复选框的状态,但无法保留该号码检查了联系人。

这是我到目前为止所尝试的:

  public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        Bitmap bitmap = null;

        if(convertView == null)
        {
            holder = new Holder();

            convertView = layoutInflater.inflate(R.layout.fetch_single_contact, null);
            holder.name = (TextView) convertView.findViewById(R.id.sc_name);
            holder.number = (TextView) convertView.findViewById(R.id.sc_number);

            holder.name.setTypeface(mTypeface);
            holder.number.setTypeface(mTypeface);

            holder.select = (CheckBox)convertView.findViewById(R.id.contact_checkbox_btn);

            holder.contact_img = (ImageView)convertView.findViewById(R.id.contact_img);

            holder.select.setOnCheckedChangeListener(new OnCheckedChangeListener()
            {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                {
                    int getPosition = (Integer)buttonView.getTag();

                    contactsList.get(getPosition).setSelected(buttonView.isChecked());

                    if(isChecked)
                    {
                        total_contacts_selected ++;
                    }
                    else if(!isChecked)
                    {
                        total_contacts_selected--;
                    }
                    if(total_contacts_selected > 5)
                    {
                        Log.i("ContactsAdapter", "5 Contacts already selected");

                    Toast.makeText(getApplicationContext(), "5 Contacts already selected", Toast.LENGTH_LONG).show();
                        buttonView.setChecked(false);
                        total_contacts_selected--;
                    }
                    else
                    {
                contactsList.get(getPosition).setSelected(buttonView.isChecked());
                    }

                }
            });

            convertView.setTag(holder);
            convertView.setTag(R.id.sc_name, holder.name);
            convertView.setTag(R.id.contact_checkbox_btn, holder.select);

        }

        else{
            holder = (Holder) convertView.getTag();
        }

        holder.name.setText(contactsList.get(position).getName());
        holder.number.setText(contactsList.get(position).getNumber());
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(contactsList.get(position).getImgUri()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        holder.contact_img.setImageBitmap(getRoundedCornerBitmap(bitmap, 100));
        holder.select.setTag(position);
        holder.select.setChecked(contactsList.get(position).isSelected());
        return convertView;
    }

2 个答案:

答案 0 :(得分:1)

当您致电holder.select.setChecked()时,计数器会更新。基本上每次调用getView()都会搞乱计数器。最简单的解决方案是在getView()中设置检查状态之前删除侦听器,然后再将其放回。您可以将其另存为成员,以避免每次都重新创建新的侦听器。

答案 1 :(得分:0)

更改onCheckedChanged方法如下。如果先前和当前状态相同,则应忽略检查更改事件。

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int getPosition = (Integer) buttonView.getTag();
        boolean isSelected = contactsList.get(getPosition).isSelected();
        if (isSelected == isChecked) {
            // Current state is same as previous one. Ignore.
            return;
        }
        if (isChecked) {
            total_contacts_selected++;
        } else if (isChecked == false) {
            total_contacts_selected--;
        }
        if (total_contacts_selected > 5) {
            Log.i("ContactsAdapter", "5 Contacts already selected");
            Toast.makeText(getApplicationContext(),
                    "5 Contacts already selected", Toast.LENGTH_LONG).show();
            // When we call below method, onCheckChanged will be invoked again.
            // First if case in will make sure that un-necessary code will not
            // be executed.
            buttonView.setChecked(false);
            total_contacts_selected--;
        } else {
            contactsList.get(getPosition).setSelected(isChecked);
        }
    }