1

I have a ListView with let's say 30 items.

For the items, I'm using a custom adapter, which I store in an instance variable of the containing activity.

Now I want to cache the content (my list with model objects) in some cases. It means if I resume the activity nothing has to change.

If the model is cached I assume I don't need to redraw the view because nothing has changed.

Without caching I call this each time, in onResume:

myAdapter.setMyData(new ArrayList<MyType>());
myAdapter.setFoo(new Foo());
myAdapter.notifyDataSetChanged();

And that works, the list is displayed well.

But when I don't call that (besides of the very first time this activity is resumed) (because I'm using the cache), the ListView looks strange. While normally ~ 7 items fit in the screen, now I get only ~ 2 with a very long space in between. And the assets inside the items also a bit shifted. Scrolling down fixes it, the following items look normally, and if I scroll back up everything looks fine.

Why is this?

Edit: The key of the issue is

myAdapter.notifyDataSetChanged();

If I call that each time in onResume the list is displayed correctly. But I don't understand why I have to do it, because my data set didn't change.

4

0 回答 0