Created
December 24, 2022 16:30
-
-
Save dario61081/d62cc357c84a57dd21a6d437940ab674 to your computer and use it in GitHub Desktop.
custom filter for products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NonNull | |
@Override | |
public Filter getFilter() { | |
return new Filter() { | |
@Override | |
protected FilterResults performFiltering(CharSequence charSequence) { | |
String filterString = charSequence.toString().toUpperCase(); | |
FilterResults filterResults = new FilterResults(); | |
if (filterString == null || filterString.length() == 0) { | |
filterResults.values = productos; | |
filterResults.count = productos.size(); | |
return filterResults; | |
} | |
/*listar filtrado*/ | |
List<Producto> encontrados = new ArrayList<>(); | |
for (Producto p : productos) { | |
if (p.descripcion.toUpperCase().contains(filterString)) { | |
encontrados.add(p); | |
} | |
} | |
filterResults.values = encontrados; | |
filterResults.count = encontrados.size(); | |
return filterResults; | |
} | |
@Override | |
protected void publishResults(CharSequence charSequence, FilterResults filterResults) { | |
notifyDataSetChanged(); | |
clear(); | |
addAll((Collection<? extends Producto>) filterResults.values); | |
notifyDataSetInvalidated(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment