选中复选框而不点击它们

时间:2014-04-04 05:17:16

标签: android checkbox

我已经创建了一个应用程序,其中我检查了一些复选框,以获取他们的值。但是每当我选中单个复选框时,默认情况下其他一些复选框也会在列表中打勾。我不知道问题是什么,为什么会得到检查。 的适配器

package com.example.smscampaign;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;


public class SearchableAdapter extends BaseAdapter implements Filterable, OnCheckedChangeListener {

    public SparseBooleanArray mCheckStates;
    private List<ProfileBean>originalData = null;
    private List<ProfileBean>filteredData = null;
    private LayoutInflater mInflater;
    private ItemFilter mFilter = new ItemFilter();

    public SearchableAdapter(Context context, List<ProfileBean> data) {
        //mCheckStates = new SparseBooleanArray(filteredData.size());
        mCheckStates = new SparseBooleanArray(data.size());
        this.filteredData = data ;
        this.originalData = data ;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return filteredData.size();
    }

    public Object getItem(int position) {
        return filteredData.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

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

        ViewHolder holder;


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row, null);


            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.textView1);
            holder.number = (TextView) convertView.findViewById(R.id.textView2);
            holder.chk = (CheckBox) convertView.findViewById(R.id.checkBox1);
            holder.chk.setTag(position);

            holder.chk.setChecked(mCheckStates.get(position, false));
            holder.chk.setOnCheckedChangeListener(this);


            convertView.setTag(R.layout.row,holder);
        } else {

            holder = (ViewHolder) convertView.getTag(R.layout.row);
        }



        ProfileBean bean = filteredData.get(position);
        holder.name.setText(bean.getName());
        holder.number.setText(bean.getNumber());

        convertView.setTag(bean);

        return convertView;
    }

    static class ViewHolder {
        TextView name;
        TextView number;
        CheckBox chk;
    }

    public boolean isChecked(int position) {

        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);
    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }



    public Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<ProfileBean> list = originalData;

            int count = list.size();
            final ArrayList<ProfileBean> nlist = new ArrayList<ProfileBean>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {

                ProfileBean bean =  list.get(i);
                filterableString = bean.getName();
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(bean);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredData = (ArrayList<ProfileBean>) results.values;
            notifyDataSetChanged();
        }

    }





    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        mCheckStates.put((Integer) buttonView.getTag(), isChecked);

    }




}

1 个答案:

答案 0 :(得分:2)

您使用的是ViewHolder模式,因此会在此处重复使用相同的视图,因此您需要更改getView()代码,例如..

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

    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row, null);


        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.textView1);
        holder.number = (TextView) convertView.findViewById(R.id.textView2);
        holder.chk = (CheckBox) convertView.findViewById(R.id.checkBox1);
        holder.chk.setTag(position);
        convertView.setTag(R.layout.row,holder);
    } else {
        holder = (ViewHolder) convertView.getTag(R.layout.row);
    }
    holder.chk.setOnCheckedChangeListener(null);
    holder.chk.setChecked(mCheckStates.get(position, false));
    holder.chk.setOnCheckedChangeListener(this);

    ProfileBean bean = filteredData.get(position);
    holder.name.setText(bean.getName());
    holder.number.setText(bean.getNumber());
    convertView.setTag(bean);
    return convertView;
}