Skip to content

Instantly share code, notes, and snippets.

@dario61081
Created December 24, 2022 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dario61081/d62cc357c84a57dd21a6d437940ab674 to your computer and use it in GitHub Desktop.
Save dario61081/d62cc357c84a57dd21a6d437940ab674 to your computer and use it in GitHub Desktop.
custom filter for products
@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