mercredi 10 octobre 2018

how to set check boxes to searched items using custom adapter?

I displayed the contact list using the custom list view. When a user searched the contacts for selecting. So When the user checked the selected checkbox but after the text in the search bar is removing for searching for another contact the check of the checkbox is also removed. How to do that when the text has removed the check of the checkbox is remain still until we uncheck that contact. This is my adapter class :

    public class Splitadapter extends BaseAdapter implements Filterable,CompoundButton.OnCheckedChangeListener

    {
        public SparseBooleanArray mCheckStates;
        private ArrayList<COntactsModel> _Contacts;
        private Context mContext;
        private LayoutInflater inflater;
        private ValueFilter valueFilter;
        private ArrayList<COntactsModel> mStringFilterList;

        public Splitadapter(Context context, ArrayList<COntactsModel> _Contacts) {
            super();
            mContext = context;
            this._Contacts = _Contacts;
            mStringFilterList = _Contacts;
            mCheckStates = new SparseBooleanArray(_Contacts.size());
            this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            getFilter();

        }//End of CustomAdapter constructor


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

        @Override
        public Object getItem(int position) {
            return _Contacts.get(position).getName();
        }

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

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        }

        public class ViewHolder {
            TextView textviewName;
            TextView textviewNumber;
            CheckBox checkbox;
            ImageView image;
           Button b;
            int id;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            final int pos = position;
//
            if (convertView == null) {
                holder = new ViewHolder();

                convertView = LayoutInflater.from(mContext).inflate(R.layout.list, null);
                holder.textviewName = (TextView) convertView.findViewById(R.id.name);
                holder.textviewNumber = (TextView) convertView.findViewById(R.id.mobile);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
                holder.b = (Button) convertView.findViewById(R.id.round_icon);
                holder.image = convertView.findViewById(R.id.image);
                final ViewHolder finalHolder = holder;
                holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        COntactsModel  _state = (COntactsModel) finalHolder.checkbox.getTag();
                        _state.setSelected(buttonView.isSelected());
                    }
                });

                convertView.setTag(holder);
                holder.checkbox.setTag(mStringFilterList.get(position));
//                holder.checkbox.setOnClickListener( new View.OnClickListener()
//                {
//                    public void onClick(View v)
//                    {
//                        CheckBox cb = (CheckBox) v;
//                        COntactsModel  _state = (COntactsModel) cb.getTag();
//
//                        _state.setSelected(cb.isChecked());
//                    }
//                });


            }//End of if condition
            else {


                ((ViewHolder) convertView.getTag()).checkbox.setTag(mStringFilterList.get(position));
            }//End of else
            holder = (ViewHolder) convertView.getTag();

            holder.checkbox.setId(position);
            holder.textviewName.setId(position);
            holder.textviewNumber.setId(position);
            //COntactsModel c = mStringFilterList.get(position);

            holder.textviewName.setText(_Contacts.get(position).getName());
            holder.textviewNumber.setText(_Contacts.get(position).getPhonenum());
            holder.b.setText(_Contacts.get(position).getName().substring(0,1));

            holder.checkbox.setTag(position);
            holder.checkbox.setChecked(mCheckStates.get(position, false));
            holder.checkbox.setOnCheckedChangeListener(this);
            holder.checkbox.setChecked(mStringFilterList.get(position).isSelected());
//           holder.checkbox.setText(c.getName());
//           holder.checkbox.setChecked(c.isSelected());
//           holder.checkbox.setTag(c);

            //holder.id = position;

            return convertView;
//        }//End of getView method

        }

        boolean isChecked(int position) {// it returns the checked contacts
            return mCheckStates.get(position, false);
        }

        void setChecked(int position, boolean isChecked) { //set checkbox postions if it sis checked
            mCheckStates.put(position, isChecked);
            System.out.println("hello...........");
            notifyDataSetChanged();
        }

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


//        @Override
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            if (isChecked) {
//                mCheckStates.put((Integer) buttonView.getTag(), true);
//            } else {
//                mCheckStates.delete((Integer) buttonView.getTag());
//            }
//
//        }

        @Override
        public Filter getFilter() {
            if (valueFilter == null) {

                valueFilter = new ValueFilter();

            }

            return valueFilter;
        }

        private class ValueFilter extends Filter {

            //Invoked in a worker thread to filter the data according to the constraint.
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                if (constraint != null && constraint.length() > 0) {
                    ArrayList<COntactsModel> filterList = new ArrayList<COntactsModel>();
                    for (int i = 0; i < mStringFilterList.size(); i++) {
                        if ((mStringFilterList.get(i).getName().toUpperCase())
                                .contains(constraint.toString().toUpperCase())) {
                            COntactsModel contacts = new COntactsModel();
                            contacts.setName(mStringFilterList.get(i).getName());
                            contacts.setPhonenum(mStringFilterList.get(i).getPhonenum());
                            filterList.add(contacts);
                        }
                    }
                    results.count = filterList.size();
                    results.values = filterList;
                } else {
                    results.count = mStringFilterList.size();
                    results.values = mStringFilterList;
                }
                return results;
            }


            //Invoked in the UI thread to publish the filtering results in the user interface.
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                _Contacts = (ArrayList<COntactsModel>) results.values;
                notifyDataSetChanged();
            }
        }
    }

This is my Model class :

public class COntactsModel
{
    String phonenum;
    String cname;
    boolean selected = false;

    public String getPhonenum() {
        return phonenum;
    }
    public void setPhonenum(String phonenum) {
        this.phonenum = phonenum;
        System.out.println("se ph num"+phonenum);
    }
    public String getName() {
        return cname;
    }
    public void setName(String name) {
        this.cname = name;
    }
    public boolean isSelected()
    {
        return selected;
    }

    public void setSelected(boolean selected)
    {
        this.selected = selected;
    }
}




Aucun commentaire:

Enregistrer un commentaire