Custom Filtering ArrayAdapter in ListView

Your problem are this lines:

this.original = items;
this.fitems = items;

Items is the list you use for your ListView and putting it in two different variables does not make two different lists out of it. You are only giving the list items two different names.

You can use:

this.fitems = new ArrayList(items);

that should generate a new List and changes on this list will only change the fitems list.


you can accomplish the same effect by just creating a toString() method on your Pkmn class that returns the value you want to filter by.


The best way I found to to filter ArrayAdapter is to create my own filter class:

private class MyFilter extends Filter

then in that function create the new object array to display after the filter (you can find good implementation in the source code of class ArrayAdapter)

@Override
protected FilterResults performFiltering(CharSequence prefix)

now the trick is in this method

@Override
protected void publishResults(CharSequence constraint, FilterResults results)

when you use the Array adapter you can't do this:

myAdapterData = results.values

since then you disconnect your data from the super data, you must do this to keep your reference to the super original data array:

data.clear();
data.addAll((List<YourType>) results.values);

and then override

getFilter()

in your adapter, for example:

@Override
public Filter getFilter() {
    if (filter == null) {
        filter = new MyFilter();
    }
    return filter;
}