Custom Listview Adapter with filter Android

后端 未结 10 902
北荒
北荒 2020-11-22 06:07

Please am trying to implement a filter on my listview. But whenever the text change, the list disappears.Please Help Here are my codes. The adapter class.

p         


        
10条回答
  •  离开以前
    2020-11-22 06:44

    Just an update.

    If the ticked answer is working fine for you but it shows nothing when the search text is empty. Here is the solution:

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            
            String filterString = constraint.toString().toLowerCase();
            
            FilterResults results = new FilterResults();
    
            if(constraint.length() == 0)
            {
                results.count = originalData.size();
                results.values = originalData;
            }else {
    
            
            final List list = originalData;
    
            int count = list.size();
            final ArrayList nlist = new ArrayList(count);
    
            String filterableString ;
            
            for (int i = 0; i < count; i++) {
                filterableString = list.get(i);
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(filterableString);
                }
            }
            
            results.values = nlist;
            results.count = nlist.size();
         }
            return results;
        }
    
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredData = (ArrayList) results.values;
            notifyDataSetChanged();
          }
    
       }
    

    For any query comment below

提交回复
热议问题