1

I have ArrayList of custom objects that is the data underlying an ArrayAdapter for a ListView.

Sometimes this data is modified in a batch, such as fetching a set of new items from the web. When the data is modified in a batch, should notifyDataSetChanged() be called after every add() to the ArrayList

Some over simplified code:

for(Object object : newObjects){
   list.add(object);
   adapter.notifyDataSetChanged();
}

or should it be called once after all of the items in the batch have been added?

for(Object object : newObjects){
    list.add(object);
}
adapter.notifyDataSetChanged()

Say there is a batch of 50 new objects. If 50 notifyDataSetChanged() calls are made right after another, like in the first example, will the views redraw themselves 50 times in a row (I imagine a major performance hit) or will it only perform the latest call and in a sense only redraw them once?

I'm basically wondering if I can use the first method or will it have a major performance impact?

Thanks

4

1 回答 1

0

当您调用 notifyDataSetChanged() 时,它不会立即重绘视图。由 UI 线程控制器决定何时重绘。虽然它看起来是瞬间的。您应该尝试一下,看看是否存在性能问题。我们真的无法为你回答这个问题。如果在所有添加之后只更新列表视图是有意义的,那么就这样做。

于 2010-10-26T07:55:19.130 回答