Android Filter Custom ListView (BaseAdapter)

In the previous tutorial we have seen an example of how to filter Simple Listview which uses ArrayAdapter , when it comes to custom listview adding search filter it’s typically different not so easy as simple listview because in custom listview each row contains many data (also termed as constrains) , to filter custom listview you need to create your own custom filter for a constrains by which you can filter it

Before I start coding i kept all the necessary images inside the drawable folder and also declared array of string inside value.xml which you can find inside the download code link below .

Custom Adapter and Custom Filter

Custom Filter We have seen in previous example of filtering simple listview where we used default filter , when it comes to custom listview we need custom filter the reason for this is items of custom listview is associated with many data (which we call it as constrains) so we need to decide on which constrain the listview should be filtered .

How to Create Custom filter

Implement the Filterable Interface and override the getFilter() method .

Inside Create FilterResults Object and write a logic on which constrain you would like to filter listview (here in this example the listview is filter by country name so here country is checked with the constraint) .

Main Activity

Create MainActivity.java and extend this class to activity class and set the content of this activity to above defined activity_main.xml file .

Setup the ListView with above defined baseadapter .

Set onQueryTextListeners for SearchView , to set this you need to implement SearchView.OnQueryTextListener interface for your class and override the two methods of it , they are onQueryTextChange and onQueryTextSubmit .