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;
}
@santhosh153
Copy link

its throwing illiagle state exception
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(-1, class android.widget.ListPopupWindow$DropDownListView) with Adapter(class com.xxxxxx.xxxxx.PlacesSeacrhFragment$GooglePlacesAdapter)]
E/AndroidRuntime(16820): at android.widget.ListView.layoutChildren(ListView.java:1562)
E/AndroidRuntime(16820): at android.widget.AbsListView.onLayout(AbsListView.java:2151)
E/AndroidRuntime(16820): at android.view.View.layout(View.java:15671)
E/AndroidRuntime(16820): at android.view.ViewGroup.layout(ViewGroup.java:5038)
E/AndroidRuntime(16820): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
E/AndroidRuntime(16820): at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
E/AndroidRuntime(16820): at android.view.View.layout(View.java:15671)
E/AndroidRuntime(16820): at android.view.ViewGroup.layout(ViewGroup.java:5038)
E/AndroidRuntime(16820): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
E/AndroidRuntime(16820): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
E/AndroidRuntime(16820): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
E/AndroidRuntime(16820): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
E/AndroidRuntime(16820): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
E/AndroidRuntime(16820): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
E/AndroidRuntime(16820): at android.view.Choreographer.doFrame(Choreographer.java:550)
E/AndroidRuntime(16820): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
E/AndroidRuntime(16820): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime(16820): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(16820): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(16820): at android.app.ActivityThread.main(ActivityThread.java:5254)
E/AndroidRuntime(16820): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(16820): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(16820): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
E/AndroidRuntime(16820): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

@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