1

i have create an app in which i checked some checkbox,to take their values.But whenever i checked on single checkbox by default some other checkbox also get ticked in the list.i dont knw what is the problem,why it get checked. Adapter

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
  • posted my answer check like that.. Commented Apr 4, 2014 at 5:26

1 Answer 1

2

You are using ViewHolder pattern so same view will be ReUsed in this so you need to change your getView() code like..

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.