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

时间:2016-08-01 11:32:24

标签: java android listview android-filter

花了几个小时试图让这个工作,但没有用:( 我为listview设置了一个searchfilter,工作正常。但每个都有一个复选框 - 当我在过滤之前或之后检查复选框时,它不会被记住,这使得过滤器毫无意义。非常感谢这里的一些指导。

我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SearchView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements
        SearchView.OnQueryTextListener {

    private AllStuffModel[] modelItems = AllStuff.allConditions;
    private ListView conditionsView;
    private SearchView search_view;
    private AllStuffAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adapter = new AllStuffAdapter(this, new ArrayList<AllStuffModel>(Arrays.asList(modelItems)), this);
        conditionsView = (ListView) findViewById(R.id.condition_list);
        conditionsView.setAdapter(adapter);

        search_view = (SearchView) findViewById(R.id.search_view);
        search_view.setOnQueryTextListener(this);

    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

}

AllStuffAdapter.java

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

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

public class AllStuffAdapter extends ArrayAdapter<AllStuffModel> implements Filterable {
    private List<AllStuffModel> modelItems;
    private List<AllStuffModel> filteredModelItems;
    private Context context;
    private MainActivity mainActivity;
    private ValueFilter valueFilter;

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public AllStuffAdapter(Context context, List<AllStuffModel> resource, MainActivity mainActivity) {
        super(context, R.layout.condition_row, resource);
        this.context = context;
        this.modelItems = resource;
        this.mainActivity = mainActivity;
        filteredModelItems = modelItems;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.condition_row, parent, false);
        TextView conditionText = (TextView) convertView.findViewById(R.id.condition_text);
        final CheckBox conditionCheckBox = (CheckBox) convertView.findViewById(R.id.condition_checkbox);
        conditionCheckBox.setId(position);
        conditionText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!conditionCheckBox.isChecked()) {
                    conditionCheckBox.setChecked(true);
                    modelItems.get(position).setIsChecked(true);
                }
                else if(conditionCheckBox.isChecked()) {
                    conditionCheckBox.setChecked(false);
                    modelItems.get(position).setIsChecked(false);
                }
                System.out.println( modelItems.get(position).isChecked());
            }
        });


        conditionText.setText(modelItems.get(position).getName());
        conditionCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox currentCheckBox = (CheckBox) v;
                if (currentCheckBox.isChecked()) { // If checked set to true, else false
                    modelItems.get(position).setIsChecked(true);
                } else if (!currentCheckBox.isChecked()) {
                    modelItems.get(position).setIsChecked(false);
                }
                System.out.println( modelItems.get(position).isChecked());
            }
        });

        return convertView;
    }

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

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

    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if (constraint != null && constraint.length() > 0) {
                ArrayList<AllStuffModel> filterList = new ArrayList<AllStuffModel>();
                for (int i = 0; i < filteredModelItems.size(); i++) {
                    if ((filteredModelItems.get(i).getName().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {

                        AllStuffModel allConditionsModel = new AllStuffModel(filteredModelItems.get(i).getCode(),filteredModelItems.get(i).getName());

                        allConditionsModel.setIsChecked(filteredModelItems.get(i).isChecked());

                        filterList.add(allConditionsModel);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = filteredModelItems.size();
                results.values = filteredModelItems;
            }
            return results;

        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            modelItems = (ArrayList<AllStuffModel>) results.values;
            notifyDataSetChanged();
        }
    }

}

AllStuffModel.java

    public class AllStuffModel {
        private String name;
        private String code;
        private boolean isChecked;


        public AllStuffModel(String code, String name) {
            this.name = name;
            this.code = code;
        }


        public String getName() {
            return this.name;
        }


        public String getCode() {
            return this.code;
        }


        public boolean isChecked() {
            return isChecked;
        }


        public void setIsChecked(boolean isChecked) {
            this.isChecked = isChecked;
        }
    }

AllStuff.java

    public class AllStuff {

        private AllStuff() {

        }

        public static final AllStuffModel[] allConditions = {
                new AllStuffModel("ca", "Candy"),
                new AllStuffModel("fo", "Football"),
                new AllStuffModel("mu", "Music"),
                new AllStuffModel("sn", "Snooker"),
        };

    }

condition_row.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingTop="10dp">

        <CheckBox
            android:id="@+id/condition_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="7dp" />

        <TextView
            android:id="@+id/condition_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#DE000000"
            android:textSize="17sp" />

    </LinearLayout>

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/scrollview_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:queryHint="Search...." />




        <ListView
            android:id="@+id/condition_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></ListView>


    </LinearLayout>

    </ScrollView>

我尝试做的是在notifyDataSetChaanged()中的onClick方法中使用AllStuffAdapter,但这会阻止完全检查复选框。

TL / DR:如何记住使用搜索过滤器检查的复选框?

2 个答案:

答案 0 :(得分:3)

尝试更改

conditionCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox currentCheckBox = (CheckBox) v;
            if (currentCheckBox.isChecked()) { // If checked set to true, else false
                modelItems.get(position).setIsChecked(true);
            } else if (!currentCheckBox.isChecked()) {
                modelItems.get(position).setIsChecked(false);
            }
            System.out.println( modelItems.get(position).isChecked());
        }
    });

conditionCheckBox.setOnCheckedChangeListener(null);
conditionCheckBox.setChecked(modelItems.get(position).isChecked());
conditionCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            modelItems.get(position).setIsChecked(isChecked);
        }
    });

答案 1 :(得分:2)

感谢@faranjit,我相信在modelItems.get(position).setIsChecked(isChecked);之后添加此内容(以及您的更改)可以完全解决问题。这可以验证吗?

for(AllStuffModel item: filteredModelItems){
                    if(modelItems.get(position).getCode().equals(item.getCode())){
                        item.setIsChecked(isChecked);
                    }
                }

我们只是通过匹配正确的项目来更新列表。注意:code是我自己设定的,我总能保证它是独一无二的。