Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 thought 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?

like image 287
emmby Avatar asked Apr 19 '10 23:04

emmby


1 Answers

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.

like image 149
Peter Knego Avatar answered Sep 21 '22 23:09

Peter Knego