How to override filter in android's ArrayAdapter?
I have an ArrayAdapter wrapped around an ArrayList of custom objects. I'd like to write a custom filter for that adapter so that when I call getListAdapter().getFilter().filter("abc")
the list will get filtered by an arbitrary transformation of "abc".
I th开发者_开发技巧ought I would just try to override ArrayAdapter.getFilter()
, but that requires I re-implement the private ArrayAdapter.ArrayFilter
which requires access to a bunch of ArrayAdapter's private instances.
What's the simplest way to do this?
First, take a look at the source code of ArrayAdapter.
You'll notice that it has private field mFilter
that's only used in getFilter()
method. So, just extend ArrayAdapter
and override getFilter()
to return your Filter
.
It's best to implement your Filter
the same way as ArrayFilter
: as private inner class, so it has access to private fields of ArrayAdapter
.
Let me know if this is enough info to complete the task.
精彩评论