[Android] Filtering a ListView using an ArrayAdapter

The contents ListView can be filtered by the users input when it is enabled. The filter input is enabled by calling its setTextFilterEnabled method. When this is called, the next sequence of user’s input will taken as the filter for the list. The initial contents of the filter is set programatically by using the setFilterText method.

The data in the list is determined by a given ListAdapter. If the ListView was using an ArrayAdapter with strings, the filter would simply filter out those strings. However, if the data was some other type of object, the filter will filter the objects based on the string that is returned by calling the object’s toString method.

To see this, you can view the example that can be downloaded here. The example application is the same as the one used in Using an ArrayAdapter to Control a ListView’s Data, except that the menu now has a “Filter” option.

The item’s in the list are represented by instances of Item. The class is defined as follows:

public class Item
    private final String name;
    private final Double price;

    public Item(String name, Double price)
        this.name = name;
        this.price = price;

    public String getName()
        return name;

    public Double getPrice()
        return price;

    public String toString()
        return name;

The Item class defines toString to return the name of the item. Consequently, the filter will filter objects based on the name of the items.


7 Responses to [Android] Filtering a ListView using an ArrayAdapter

  1. Chris says:

    I’m confused – there’s no keyboard to type the filter in?

    • kahgoh says:

      Hmm … Before today, I’ve only runned this demo on an emulator and never really thought about trying it on a device without a keyboard… Anyway, long pressing the menu button is supposed to bring open the virtual keyboard. Let me know how it goes!

      I’ll try adding a post on another way of providing list filtering within the next couple of days!

      • kahgoh says:

        I should have also mentioned that you can also programmatically display the virtual keyboard, like this:

        final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
          imm.showSoftInput(getListView(), 0);
    • kahgoh says:

      Took me a bit longer than I thought it would, but I’ve added Allowing the user to enter a filter for a ListView.

  2. Pingback: [Android] Allowing the user to enter a filter for a ListView « Kah – The Developer

  3. Pingback: [Android] Allowing the user to enter a filter for a ListView « Kah – The Developer

  4. Edson Gregorio says:

    OMG!!!! You simplily facilitated my life strongly!! Thank you so mutch for this post!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: