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) {
        this.name = name;
        this.area = area;
        this.population = population;
    }
    
    public int getArea() {
        return area;
    }
    
    public String getName() {
        return name;
    }
    
    public int getPopulation() {
        return population;
    }
    
    @Override
    public String toString() {
        return this.name;
    }

}

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.app.Activity;
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(R.id.textViewItemCountryName);
            this.textViewItemCountryArea = (TextView) v.findViewById(R.id.textViewItemCountryArea);
            this.textViewItemCountryPopulation = (TextView) v.findViewById(R.id.textViewItemCountryPopulation);
        }
    }

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

        final Country item = getItem(position);
        if (item != null) {
            holder.textViewItemCountryName.setText(Html.fromHtml(item.getName()));
            holder.textViewItemCountryArea.setText(Html.fromHtml("area = " + item.getArea() + " sq.km"));
            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.app.Activity;
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;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new CountryListAdapter(this, countries);
        
        editTextFilter = (EditText)findViewById(R.id.editTextFilter);
        editTextFilter.addTextChangedListener(this);
        
        listViewCountries = (ListView)findViewById(R.id.listViewCountries);
        listViewCountries.setAdapter(adapter);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s);
    }

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

}

That’s all.

Download it from github

0 Comments

Leave a Reply

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.