在SearchView中过滤无法在Android中运行

时间:2017-04-03 08:01:35

标签: android filter android-recyclerview searchview android-filterable

我正在开发一个搜索功能,如果我在android中的搜索视图中输入搜索关键字,我应该得到与我的搜索词匹配的学生列表。我写了一段代码,但收到了错误。如果我以正确的方式使用适配器,请告诉我。我的onCreateOptionsMenu函数

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.options_menu, menu);
    SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =(SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));



    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            StudentAdapter.getFilter().filter(newText);
            return true;

        }
    });

    return true;
}

我的studentAdapter课程:

public class StudentAdapter extends
    RecyclerView.Adapter<StudentAdapter.MyViewHolder> implements
    Filterable {

public List<StudentModel> new_list;
private List<StudentModel> original_list;

@Override
public Filter getFilter() {
    return new Filter() {
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            new_list = (List<StudentModel>) results.values;
            StudentAdapter.this.notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults initial_results = new FilterResults();

            if (constraint == null || constraint.length() == 0) {
                initial_results.values = original_list;
                initial_results.count = original_list.size();
            } else {

                // We perform filtering operation
                List<StudentModel> filteredList = new ArrayList<StudentModel>();

                // breaking string into words algorithm
                String str = constraint.toString();
                ArrayList<String> arrayOfWords = new ArrayList<String>();
                int i = 0;
                for (String word : str.split(" ")) {
                    arrayOfWords.add(word);
                    i++;
                }

                for (StudentModel model : original_list) {
                    boolean matched = false;

                    for (String element : arrayOfWords) {


                        if ((null != model.getName() && model
                                .getName().toUpperCase()
                                .contains(element.toString().toUpperCase()))


                                || ((null != model.getClass() && model
                                .getClass()
                                .toUpperCase()
                                .contains(
                                        element.toString()
                                                .toUpperCase())))) {
                            matched = true;
                        } else {
                            matched = false;
                            break;
                        }

                    }

                }

                initial_results.values = filteredList;
                initial_results.count = filteredList.size();

            }

            return initial_results;
        }
    };
    // return null;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name, class;

    public MyViewHolder(View view) {
        super(view);
        name = (TextView) view.findViewById(R.id.name);
        class = (TextView) view.findViewById(R.id.class);


    }
}

public StudentAdapter(List<StudentModel> original_list) {

    this.original_list = original_list;
}

public List<StudentModel> getStudentlist() {
    return original_list;
}

@Override
public StudentAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.original_list_row, parent, false);

    return new StudentAdapter.MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(StudentAdapter.MyViewHolder holder,
                             int position) {
    StudentModel original_list_vo = original_list.get(position);

    holder.name.setText(original_list_vo.getName());
    holder.class.setText(original_list_vo.getClass());


}

@Override
public int getItemCount() {
    return original_list.size();
}

}

任何帮助将不胜感激,提前谢谢。

1 个答案:

答案 0 :(得分:0)

在onCreate中声明您的搜索视图。

sv= (android.widget.SearchView) findViewById(R.id.searchViewid);    

sv.setIconifiedByDefault(false);
    sv.setOnQueryTextListener(this);
    sv.setSubmitButtonEnabled(true);
    sv.setQueryHint("Search Here");

现在过度使用这些方法。

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

@Override
public boolean onQueryTextChange(String newText)
{
    android.widget.Filter filter = YourAdapter.getFilter();
    if (TextUtils.isEmpty(newText)) {
        YourListView.clearTextFilter();
        YourAdapter.getFilter().filter("");
    } else {
        YourListView.setFilterText(newText);
    }
    filter.filter(newText);
    return true;
}

试试这个并告诉输出结果。