[ACCEPTED]-Custom Filtering ArrayAdapter in ListView-android-arrayadapter

Accepted answer
Score: 18

Your problem are this lines:

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

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

You 3 can use:

this.fitems = new ArrayList(items);

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

Score: 2

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

Score: 1

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

private class MyFilter extends Filter

then in 10 that function create the new object array 9 to display after the filter (you can find 8 good implementation in the source code of 7 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 6 use the Array adapter you can't do this:

myAdapterData = results.values

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

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

and then 2 override

getFilter()

in your adapter, for 1 example:

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

They have it covered here: https://www.mysamplecode.com/2012/07/android-listview-custom-layout-filter.html

You'll need 6 to call clear() on the arrrat adapter to 5 not hit the indexBoundException and add() on 4 every element, since addAll() is valid only 3 since honeycomb.

@SuppressWarnings("unchecked") @Override protected 2 void publishResults(CharSequence constraint, FilterResults 1 results) {

countryList = (ArrayList<Country>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = countryList.size(); i < l; i++)
 add(countryList.get(i));
notifyDataSetInvalidated();

}

More Related questions