Android 列表视图中的复选框自动选择

Android Checkbox in Listview selecting automatically

在我的 android 应用程序中,我在列表视图中显示 phone 的联系人列表,并在每行中显示一个用于选择联系人的复选框。但是当我选择特定行时,大约第十行也会自动被选中。我在下面给出我的代码,如果有人知道请帮助..

public class ContactsAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<Contact> contacts;


    public ContactsAdapter(Context context, ArrayList<Contact> contacts)
    {
        this.context = context;
        this.contacts = contacts;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        final ViewHolder mHolder;
        if (convertView == null)
        {

            // gridView = new View(context);
            // get layout from mobile.xml
            //gridView = inflater.inflate(R.layout.contact, null);
            convertView = inflater.inflate(R.layout.contact, null);
            mHolder = new ViewHolder();

            mHolder.textName     =(TextView) convertView.findViewById(R.id.name);
            mHolder.textMobile   =(TextView) convertView.findViewById(R.id.mobile);
            mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);

            convertView.setTag(mHolder);


));
            }



        else
        {


            mHolder = (ViewHolder) convertView.getTag();
            mHolder.textSelector.setOnCheckedChangeListener(null);
        }

        mHolder.textMobile.setText(contacts.get(position).getMobile());
        mHolder.textName.setText(contacts.get(position).getName());
        mHolder.textSelector.setFocusable(false);


        return convertView;
    }




    private class ViewHolder {
        private TextView textMobile,textName;
        private CheckBox textSelector;


    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

那是因为 ViewHolder 会在你每次滚动时回收视图 我建议你在 ListItem 上使用 onCLick 而不是 checkbox

克服这个问题声明一个 SparseBooleanArray

SparseBooleanArray sba=new SparseBooleanArray();//this should be global

然后在渲染后立即设置项目检查状态

 mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
 mHolder.textSelector.setChecked(sba.get(position));

然后写一个onClickListener给你convertView,手动查看

 convertView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            mHolder.textSelector.setChecked(isChecked);
            sba.put(position,isChecked); //storing the state
           }
       }

);  

   **Well Now the sba has list items checked and you can use that for further Actions**

@Jocheved...像这样编辑您的代码...

public class ContactsAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<Contact> contacts;
    SparseBooleanArray sba=new SparseBooleanArray();

    public ContactsAdapter(Context context, ArrayList<Contact> contacts)
    {
        this.context = context;
        this.contacts = contacts;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final ViewHolder mHolder;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.contact, null);
            mHolder = new ViewHolder();

            mHolder.textName     =(TextView) convertView.findViewById(R.id.name);
            mHolder.textMobile   =(TextView) convertView.findViewById(R.id.mobile);
            mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected); 

            convertView.setTag(mHolder);   

            } 
        else
        {
            mHolder = (ViewHolder) convertView.getTag();
        }

        mHolder.textMobile.setText(contacts.get(position).getMobile());
        mHolder.textName.setText(contacts.get(position).getName());
        mHolder.textName.setSelected(true);
        mHolder.textSelector.setChecked(sba.get(position));

        mHolder.textSelector.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {

                 if(mHolder.textSelector.isChecked())
                 {
                     sba.put(position, true);
                 }

                else
                {
                    sba.put(position, false);
                }

            }
        });

        return convertView;
    }

    private class ViewHolder 
{
        private TextView textMobile,textName;
        private CheckBox textSelector;


    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}