Android SearchView Filter for List View

2019-06-24 04:10发布

I have an Activity which has a Action Bar on the top with a Search View. Also i am using Custom List View and want to filter from the key words entered in the search view should be associated with a particular text view in the List View Item.

enter image description here

1条回答
Ridiculous、
2楼-- · 2019-06-24 04:36

in your onQueryTextChange(String Text) method of Listener use: adapter.getFilter().filter(Text.toString()); and implement your filter in your BaseAdapter class. here is the sample code:

 @Override
        public Filter getFilter() 
        {
             return filter;
        }

     private GameFilter filter;

     private class GameFilter extends Filter 
     {
         public GameFilter() { }
            @Override
                  protected FilterResults performFiltering(CharSequence constraint) {
                       FilterResults oReturn = new FilterResults();
                      ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
                    if (orig == null)
                      orig = itemDetailsrrayList;                    
                      if (constraint != null)
                   {
                        if (orig != null && orig.size() > 0) {
                          for (ItemDetails g : orig) {
                                  if (g.getName().toLowerCase().contains(constraint.toString().toLowerCase()))
                                 results.add(g);
                        }
                   }
                       oReturn.values = results;
                    }
                     return oReturn;
                  }

                 @SuppressWarnings("unchecked")
                 @Override
                protected void publishResults(CharSequence constraint, FilterResults results)
                 {
                     itemDetailsrrayList = (ArrayList<ItemDetails>)results.values;
                   notifyDataSetChanged();
                 }
            }
查看更多
登录 后发表回答