ListView Not Updating After Filtering

Viewed 11041

I have a ListView (with setTextFilterEnabled(true)) and a custom adapter (extends ArrayAdapter) which I update from the main UI thread whenever a new item is added/inserted. Everything works fine at first--new items show up in the list immediately. However this stops the moment I try to filter the list.

Filtering works, but I do it once and all of my succeeding attempts to modify the contents of the list (add, remove) don't display anymore. I used the Log to see if the adapter's list data gets updated properly, and it does, but it's no longer in sync with the ListView shown.

Any ideas what's causing this and how best to address the issue?

5 Answers

I was struggling with the same issue. Then I found this post on StackOverflow.

In the answer @MattDavis posted, the incoming arraylist is assigned to 2 different arraylists.

 public PromotionListAdapter(Activity a, ArrayList<HashMap<String, String>> d) 
{
    activity = a;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());

    //To start, set both data sources to the incoming data
    originalData = d;
    filteredData = d;
}

originalData and filteredData.

originalData is where the original arraylist is stored that was passed in, while filtered data is the dataset that is used when getCount and getItem are called.

In my own ArrayAdapter I kept returning the original data when getItem was called. Therefore the ListView basically would call getCount() and see that the amount of items match my original data (not the filtered data) and getItem would get the requested indexed item from the original data, therefore completely ignoring the new filtered dataset.

This is why it seemed like the notifyDatasetChanged call was not updating the ListView, when in fact it just kept updating from the original arraylist, instead of the filtered set.

Hope this helps clarify this problem for someone else in the future.

Please feel free to correct me if I am wrong, since I am still learning every day!

Related