如何在使用带有自定义过滤器和自定义适配器的searchview搜索之后,如何记住listview中选中的复选框

时间:2019-05-22 12:10:19

标签: java android

当我在listview中搜索文本时,搜索后选中的checkboxes会自动取消选中

在搜索一切正常之前

我已经尝试过该主题

记住使用搜索过滤器选中了哪个复选框

适配器类

public class StudentsListViewForGroupsBaseAdapter extends BaseAdapter implements Filterable {

    public LayoutInflater inflater;


    public Activity context;

    ArrayList<StudentsForGroup> players;
    public static ArrayList<StudentsForGroup> players1;

    CustomFilter filter;
    ArrayList<StudentsForGroup> filterList;

    public StudentsListViewForGroupsBaseAdapter(Activity context, ArrayList<StudentsForGroup> players)
    {
        //super(context, R.layout.students_listview_row, classname);
        this.context=context;
        this.players=players;
        this.filterList=players;
    }
    @Override
    public int getCount() {
        return players.size();
    }

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

    @Override
    public long getItemId(int position) {
        return players.indexOf(getItem(position));
    }



    private class ViewHolder
    {
        TextView titleText;
        TextView subtitleText;
        CheckBox checkBox;
    }

    ViewHolder holder=null;
    @Override
    public View getView(final int position,View view,ViewGroup parent) {




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

        if(view==null)
        {
            view=inflater.inflate(R.layout.students_listview_for_groups_row, null);
        }
        // LayoutInflater inflater=context.getLayoutInflater();
        //View rowView=inflater.inflate(R.layout.students_listview_row, null,true);

        TextView titleText = (TextView) view.findViewById(R.id.txtstudname);
        //ImageView imageView = (ImageView) view.findViewById(R.id.imgViewLogo);
        TextView subtitleText = (TextView) view.findViewById(R.id.txtstudetail);

        //titleText.setText(players.get(position).getStudFname()+" " +players.get(position).getStudMname()+ " " +players.get(position).getStudLname() );
        titleText.setText(players.get(position).getStudFname());

        //imageView.setImageResource(Drawable.);

        //subtitleText.setText("PRN:"+players.get(position).getStudPrn()+ ", RNO: " + players.get(position).getStudRollno()+ ", Class: " + players.get(position).getClassName()+ ", Division: " + players.get(position).getDivName());
        subtitleText.setText("PRN:"+players.get(position).getStudPrn()+ ", RNO: " + players.get(position).getStudRollno()+ ", Class: " + players.get(position).getClassName());

        CheckBox checkBox=(CheckBox)view.findViewById(R.id.checkboxstudent);

        checkBox.setOnCheckedChangeListener(null); //important
        checkBox.setChecked(players.get(position).isSelected());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean checked)
            {
                //players.get(position).setSelected(checked);
                if(checked) {
                    players.get(position).setSelected(true);
                    //filterList.get(position).setSelected(true);
                }
                else {
                    players.get(position).setSelected(false);
                    //filterList.get(position).setSelected(false);
                }

                Toast.makeText(context,"Selected:" + players.get(position).isSelected(),Toast.LENGTH_LONG).show();
            }
        });



        for(StudentsForGroup item: filterList){
            if(players.get(position).getStudPrn().equals(item.getStudPrn())){
                item.isSelected();
            }
        }


        return view;



    }




    @Override
    public Filter getFilter() {
        if(filter == null)
        {
            filter=new CustomFilter();
        }

        players=filterList;

        return filter;
    }

    //INNER CLASS
    class CustomFilter extends Filter
    {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // TODO Auto-generated method stub

           // filterList=players;

            FilterResults results=new FilterResults();

            if(constraint != null && constraint.length()>0)
            {
                //CONSTARINT TO UPPER
                constraint=constraint.toString().toUpperCase();

                ArrayList<StudentsForGroup> filters=new ArrayList<StudentsForGroup>();

                //get specific items
                for(int i=0;i<filterList.size();i++)
                {
                    if(filterList.get(i).getStudFname().toUpperCase().contains(constraint) || filterList.get(i).getStudLname().toUpperCase().contains(constraint) || filterList.get(i).getStudLname().toUpperCase().contains(constraint) || filterList.get(i).getClassName().toUpperCase().contains(constraint)) {

                        StudentsForGroup p;
                        //if (filterList.get(i).isSelected()) {
                          //  p = new StudentsForGroup(filterList.get(i).getStudFname(), filterList.get(i).getStudMname(), filterList.get(i).getStudLname(), filterList.get(i).getStudPrn(), filterList.get(i).getStudRollno(), filterList.get(i).getClassName(), filterList.get(i).getDivName(), filterList.get(i).isSelected());

                        //} else{
                            p = new StudentsForGroup(filterList.get(i).getStudFname(), filterList.get(i).getStudMname(), filterList.get(i).getStudLname(), filterList.get(i).getStudPrn(), filterList.get(i).getStudRollno(), filterList.get(i).getClassName(), filterList.get(i).getDivName(), filterList.get(i).isSelected());
                        //}

                        p.setSelected(filterList.get(i).isSelected());

                        filters.add(p);
                    }
                }

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

            }else
            {
                results.count=filterList.size();
                results.values=filterList;

            }

            players=filterList;
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // TODO Auto-generated method stub

            players=(ArrayList<StudentsForGroup>) results.values;
            players1=(ArrayList<StudentsForGroup>) results.values;


            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                notifyDataSetChanged();
            }


          //  notifyDataSetChanged();
        }

    }
 }

学生模型

public class StudentsForGroup {

    private String studMname;
    private String studLname;
    private String studPrn;
    private String divName;
    private String studFname;
    private String studRollno;

    private String className;


    private  boolean isSelected=false;

    public StudentsForGroup(String studFname, String studMname, String studLname, String studPrn, String studRollno, String className, String divName, Boolean isSelected) {
    this.studFname=studFname;
    this.studMname=studMname;
    this.studLname=studLname;
    this.studPrn=studPrn;
    this.studRollno=studRollno;
    this.className=className;
    this.divName=divName;

    this.isSelected=isSelected;
    }

    public void setStudMname(String studMname){
        this.studMname = studMname;
    }

    public String getStudMname(){
        return studMname;
    }

    public void setStudLname(String studLname){
        this.studLname = studLname;
    }

    public String getStudLname(){
        return studLname;
    }

    public void setStudPrn(String studPrn){
        this.studPrn = studPrn;
    }

    public String getStudPrn(){
        return studPrn;
    }

    public void setDivName(String divName){
        this.divName = divName;
    }

    public String getDivName(){
        return divName;
    }

    public void setStudFname(String studFname){
        this.studFname = studFname;
    }

    public String getStudFname(){
        return studFname;
    }

    public void setStudRollno(String studRollno){
        this.studRollno = studRollno;
    }

    public String getStudRollno(){
        return studRollno;
    }

    public void setClassName(String className){
        this.className = className;
    }

    public String getClassName(){
        return className;
    }




    public boolean isSelected() {
        return isSelected;
    }

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




    @Override
    public String toString(){
        return 
            "Student{" + 
            "stud_mname = '" + studMname + '\'' + 
            ",stud_lname = '" + studLname + '\'' + 
            ",stud_prn = '" + studPrn + '\'' + 
            ",div_name = '" + divName + '\'' + 
            ",stud_fname = '" + studFname + '\'' + 
            ",stud_rollno = '" + studRollno + '\'' + 
            ",class_name = '" + className + '\'' + 
            "}";
        }
}

片段代码

lview = (ListView) view.findViewById(R.id.studentlistView);
            adapter = new StudentsListViewForGroupsBaseAdapter (getActivity(),getPlayers1());
            lview.setAdapter(adapter);
            lview.setOnItemClickListener(this);





    private ArrayList<StudentsForGroup> getPlayers()
    {
        players=new ArrayList<StudentsForGroup>();
        StudentsForGroup p;

        for(int i=0;i<fname.length;i++)
        {
            //p=new Student(fname[i], mname[i],lname[i],stud_prn[i],rno[i],classname[i],division[i]);
            String fullname=fname[i]+" " +mname[i]+" "+ lname[i];
            String fullname1=lname[i]+" "+fname[i]+" " +mname[i];
            String classnamediv=classname[i]+" "+ division[i];
            p=new StudentsForGroup(fullname , mname[i],fullname1,stud_prn[i],rno[i],classnamediv,division[i],false);
            players.add(p);
        }

        return players;
    }

0 个答案:

没有答案