6

I have a ListView on my Android application that I dynamically change the data of. I use the onFilterComplete() method to change the contents of the ListView.

Pre Ice Cream Sandwhich the following code works fine:

if(adapter != null) {

  adapter.notifyDataSetInvalidated();
  lview3.invalidateViews();

  adapter.getFilter().filter(aa1.getItem(item), new Filter.FilterListener() {
  public void onFilterComplete(int count) {
    adapter.notifyDataSetChanged();

    if(lview3.getCount() == 0){
      lview3.setVisibility(View.GONE);                                     
    }
    else{
      lview3.setVisibility(View.VISIBLE);
    }
}});

However on Ice Cream Sandwhich when I use the filter the screen doesn't get refreshed properly, If the filter returns a number of entries that is smaller than the previous ListView then the old list data appears to be still visible in the background, as per this screen shot:

enter image description here

From the screen shot you can see where the first entry in the ListView is, this is all that should be visible, you can see where the previous results are still visible underneath, these are just visible they are not functional as in they can't be tapped, it's as if the screen hasn't properly refreshed.

When I select the home button to leave the application via the home screen and return everything appears as it should as in the following screen shot:

enter image description here

Is there something else I have to implement to properly refresh the ListView on Ice Cream Sandwhich? Has anyone else encountered a similar issue?

What I have works fine pre ICS.

Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191
  • When you say the "old listview", you really just mean the old data in the adapter, right? Or are there multiple `ListView`s on top of each other? – Jason Robinson Jun 18 '12 at 15:38
  • Sorry yes, I mean the last filtered data in the adapter, there is only one ListView in the layout and only ever one ListView in code, not multiple., When i leave the screen and return the ListView appears as it should in the second screen shot. – Donal Rafferty Jun 18 '12 at 15:40
  • As I see it, the filter should already call datasetChanged on the adapter so you probably don't have to do that. I don't know if this could explain your problem but it's worth commenting out and trying it. – Pedro Loureiro Jun 18 '12 at 15:44
  • Can you explain why you have two adapters? `ArrayAdapter aa1` and a field `adapter`. What is its class? – Pedro Loureiro Jun 18 '12 at 15:47
  • Unfortunately commenting out setDataSetChanged does not have any effect, as for the ArrayAdapter, I've removed that sorry, I was using that to populate a pop up dialog. The user than selects the option to filter from the pop up dialog and this string is used in the filter. – Donal Rafferty Jun 18 '12 at 15:51
  • Which one is the adapter that you assigned to the listview? I was assuming that it was the `adapter` field but now I'm not so sure. – Pedro Loureiro Jun 18 '12 at 16:15
  • Yes its the variable named adapter, it is the one assigned to the list view - lview3.setAdapter(adapter); the aa1 adapter simply provides the value for the filter - adapter.getFilter().filter(aa1.getItem(item), new Filter.FilterListener()... – Donal Rafferty Jun 18 '12 at 16:17

1 Answers1

0

To fix this I changed my code to the following:

if(adapter != null) {
  adapter.getFilter().filter(aa1.getItem(item), new Filter.FilterListener() {
  public void onFilterComplete(int count) {
    adapter.notifyDataSetChanged();

    if(lview3.getCount() == 0){
      lview3.setVisibility(View.GONE);                                     
    }
    else{
      lview3.setVisibility(View.VISIBLE);
      decLayout.invalidate();
    }
}});

Where decLayout is the relative layout that my list view sits within, this seems to correctly refresh the screen when the list view data changes.

While this works I'm not 100% sure its a valid fix and I would like to hear from anyone else who has issues similar to this and may have a proper fix.

Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191