尝试无处不在,但没有在RecyclerView中获得有关SearchView的满意答案

时间:2018-05-15 23:19:54

标签: android android-studio android-recyclerview android-searchmanager

这是我的OnCreateOptionMenu  setOnQueryListener中显示错误   这里contact_object是模型类

@Override
public boolean onCreateOptionsMenu(Menu menu) {

 {
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.search_list,menu);
    final MenuItem myMenuItem=menu.findItem(R.id.search_contact);
    SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem (R.id.search_contact) .getActionView ();
    changedSearchViewTextColor(searchView);
   // ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));
     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

              if (!searchView.isIconified()){
                searchView.setIconified(true);
            }
            myMenuItem.collapseActionView();
            return false;
        }
        }
        @Override
        public boolean onQueryTextChange(String newText) {
             newText=newText.toLowerCase();
            final List<Contact_Object> filtermodelist=filter(contacts,newText);
            adapter.setFilter(filtermodelist);
            return true;
        }
    });
    return true;
  }

我已经实现了以下过滤器选项

    public List<Contact_Object> filter(List<Contact_Object> p,String query ) 
    {
    query=query.toLowerCase();
    final List<Contact_Object> filterModeList=new ArrayList<>();
    for (Contact_Object model :p){
        final String text=model.getName().toLowerCase();
        if (text.startsWith(query)){
            filterModeList.add(model);

        }
    }
    return filterModeList;
}

我在CustomAdapter类

中声明了setFilter函数
     public  void  setFilter(List<Contact_Object> listItem){
    parentList=new ArrayList<>();
    parentList.addAll(listItem);
    notifyDataSetChanged();
}

我努力但没有得到适当的答案 请任何人帮助我,等待答案

1 个答案:

答案 0 :(得分:0)

onQueryTextChange中没有逻辑,因为newText未在任何地方传递。您正在将empty列表传递给setFilter方法。

 @Override
public boolean onQueryTextChange(String newText) {
    newText = newText.toLowerCase();
    final List<Contact_Object> filterModeList=new ArrayList<>();
    for (Contact_Object obj : p) {
        String name = obj.getName().toLowerCase();
        if(name.contains(newText))
            filterModeList.add(obj);
    }
    adapter.setFilter(filtermodelist);
    return true;
}