Created
May 3, 2012 08:22
-
-
Save sabadow/2584309 to your computer and use it in GitHub Desktop.
Add a Filter on an ArrayAdapter to AutoCompleteTextView (Android)
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
@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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
did you find its solution?can you share?