Skip to content

Instantly share code, notes, and snippets.

@sabadow
Created May 3, 2012 08:22
Show Gist options
  • Save sabadow/2584309 to your computer and use it in GitHub Desktop.
Save sabadow/2584309 to your computer and use it in GitHub Desktop.
Add a Filter on an ArrayAdapter to AutoCompleteTextView (Android)
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint != null) {
List<GenericProduct> filteredProducts = new ArrayList<GenericProduct>();
for(GenericProduct currentGenericProduct : genericProductList){
if(currentGenericProduct.getName().contains(constraint)){
filteredProducts.add(currentGenericProduct);
}
}
// Now assign the values and count to the FilterResults object
filterResults.values = filteredProducts;
filterResults.count = filteredProducts.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
@ghanwah
Copy link

ghanwah commented Aug 25, 2018

did you find its solution?can you share?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment