How to create filter for ListView items in Android. Part-2. List with custom adapter.

Published by Igor Khrupin on

As you can see in my previous post about ListView filtering in Android (How to create filter for ListView items in Android.) creating a filter is pretty simple if you’re have a list with Strings.

Here I show you how to create a filter for your custom adapter with complex objects. Here the screenshot of the sample application:

Here is an updated version of this (How to create filter for ListView items in Android.) application.

In the list of countries you also can see the population and area of the country.

Please look at Country class:

package com.hrupin.sample.filterlist;

public class Country {

    private String name;
    private int area;
    private int population;

    public Country(String name, int population, int area) { = name;
        this.area = area;
        this.population = population;
    public int getArea() {
        return area;
    public String getName() {
        return name;
    public int getPopulation() {
        return population;
    public String toString() {


Note: Take a look at toString() method. This is main thing. This is method used while filtering.

Adapter for Countries list. Here is nothing new:

package com.hrupin.sample.filterlist;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CountryListAdapter extends ArrayAdapter<Country> {
    private LayoutInflater vi;
    private static int mResource = R.layout.item_list;

    public CountryListAdapter(Activity activity, final List<Country> items) {
        super(activity, mResource, items);
        vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    public static class ViewHolder {
        private TextView textViewItemCountryName;
        private TextView textViewItemCountryArea;
        private TextView textViewItemCountryPopulation;

        public ViewHolder(View v) {
            this.textViewItemCountryName = (TextView) v.findViewById(;
            this.textViewItemCountryArea = (TextView) v.findViewById(;
            this.textViewItemCountryPopulation = (TextView) v.findViewById(;

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = vi.inflate(mResource, null);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();

        final Country item = getItem(position);
        if (item != null) {
            holder.textViewItemCountryArea.setText(Html.fromHtml("area = " + item.getArea() + ""));
            holder.textViewItemCountryPopulation.setText(Html.fromHtml("population = " + item.getPopulation()));

        return convertView;

And Activity. Take a look at TextWatcher:

package com.hrupin.sample.filterlist;

import java.util.List;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListSampleActivity extends Activity implements TextWatcher {
    private static List<Country> countries = Storage.getItems();
    private EditText editTextFilter;
    private ListView listViewCountries;
    private CountryListAdapter adapter;
    public void onCreate(Bundle savedInstanceState) {
        adapter = new CountryListAdapter(this, countries);
        editTextFilter = (EditText)findViewById(;
        listViewCountries = (ListView)findViewById(;

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}


That’s all.

Download it from github


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.