SearchView Filtering and Set Suggestions

大城市里の小女人 提交于 2019-12-05 02:52:10

It seems to me that part of the problem is in the absence of the getItem() method.

If you look at the cursor adapter source code the getItem() will get the data from the cursor, and not from your filtered list, you want it to get the data from your filtered list!

Try this:

@Override
 public Object getItem(int position) {
    if (position < items.size()) {            
        return items.get(position);
    } else {
        return null;
    }
}

i forgot to add, you also need to change this:

private String getSuggestion(int position) {
    String suggest1 = (String) searchView.getSuggestionsAdapter().getItem(position);    
    return suggest1;
}

And to close everything,first add this to createOptions menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    searchView.setIconifiedByDefault(true); //this line

    ...

then add this to onSuggestionClick:

 @Override
 public boolean onSuggestionClick(int position) {
   String suggestion = getSuggestion(position);
   db.openDatabase();
   String studentId = db.getStudentIdFromName(suggestion);
   String studentImg = db.getStudentImgFromName(suggestion);
   db.closeDatabase();
   Log.e("Sp", " On Click name -> " + aptr.getItem(position).toString() + " : "
        + aptr.getCursor().getString(0) + " : " + suggestion);

   searchView.setIconified(true);//this line


   Intent i = new Intent(MainActivity.this,ExampleActivity.class);
   i.putExtra("studentId", studentId);
   i.putExtra("studentName", suggestion);
   i.putExtra("studentImg", studentImg);
   startActivity(i);
   return true;
}

A bit of an explanation. When you call setIconified it will either clear the text of your searchView or collapse it, depending on the default state. What we did there was set the default state to iconified so when we call the setIconified method it will clear the text and collapse the view!

Hello I found some problems with your code. please try my way. replace publishResults of ExampleAdapter

 @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {

                items.clear();
                 // items = new ArrayList<String>();
                //items = (ArrayList<String>) results.values;
                items.addAll((ArrayList<String>) results.values);
                notifyDataSetChanged();

            }

AND

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String arg0) {
            if (!TextUtils.isEmpty(arg0)) {
                aptr.getFilter().filter(arg0.toString());
            }
            return true;
        }

        @Override
        public boolean onQueryTextChange(String arg0) {
if (!TextUtils.isEmpty(arg0)) {
                aptr.getFilter().filter(arg0.toString());
return true;
            }
            return false;
        }
    });

Try to pass search value to filter even is empty :

@Override
public boolean onQueryTextSubmit(String arg0) {
     aptr.getFilter().filter(arg0.toString());
     return true;
}

Set default list to adapter if search not match or search value is blank :

@Override
public Filter getFilter() {
   return new Filter() {
   @Override
   protected FilterResults performFiltering(CharSequence constraint) {
      constraint = constraint.toString().toLowerCase();
      FilterResults result = new FilterResults();

      if (constraint != null && constraint.toString().length() > 0) {
          List<String> founded = new ArrayList<String>();
          for (int i = 0, l = orig.size(); i < l; i++) {
               if (orig.get(i).toString().toLowerCase().startsWith(constraint.toString().toLowerCase()))
                            founded.add(orig.get(i));
          }

          if(founded.size()>0){
             result.values = founded;
             result.count = founded.size();
          }else{
             result.values = orig;
             result.count = orig.size();
          }
      } else {
             result.values = orig;
             result.count = orig.size();
      }
      return result;

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!