ListView Not Updating After Filtering

前端 未结 5 1954
清酒与你
清酒与你 2020-12-31 06:06

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.

5条回答
  •  无人及你
    2020-12-31 06:53

    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> 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!

提交回复
热议问题