notifyDataSetChanged VS setAdapternotifyDataSetChanged、VS、setAdapter

2023-09-06 04:15:23 作者:最个性带特殊符号

我知道,这是更有效地使用notifyDataSetChanged,当我想适配器显示更新的数据。但是,由于我的设计,我对若干思考每次重置适配器,当我需要它来显示新的数据。费用多少(在执行时间)这样的决定将增加相比,使用notifidatasetChanged?

I know that it is more efficient to use notifyDataSetChanged, when I want adapter to show updated data. However, due to my design I am thinkig about resetting adapter each time, when I need it to show new data. How much cost ( in terms of execution time) such decision will add compared to using notifidatasetChanged?

推荐答案

一个有点晚了,但我不喜欢拉格纳的反应,因为它并不能说明一切。

A bit late, but I didn't like Ragnar's response, because it doesn't explain everything.

基本上,

myListAdapter.notifyDataSetChanged() 

VS

setListAdapter(new ArrayAdapter(myList));

将是pretty的性能多少类似的( notifyDataSetChanged 是不是所有的无辜:调试一步一步地看到,它触发所有变化观察 - 每下面的列表中的元素 - 向他们通报变化)

will be pretty much similar in performance (notifyDataSetChanged is not all that innocent: debug step-by-step to see that it triggers all change observers - each element of the underlying list - to inform them about changes).

性能不是你是什么后,在这种情况下。根据项目的整体结构,既可以是更多或更少的可读/可维护。主要区别不过是,通过重新创建一个适配器,你失去的状态的现有的事实。 国家的破坏与列表中的用户互动的产物 - 滚动位置,行选择,可能已交互过程中引入的更改

Performance is not what you are after in this case. Depending on the overall structure of the project, both can be more or less readable/maintainable. Main difference though, is the fact that by recreating an adapter you lose the state of the existing one. State undermines the product of user interaction with the list - scroll position, row selection, changes that may be have been introduced during interaction.

最后,如果你的设计建议,你应该重新创建和重新分配适配器,您可以只保留了执行。更可靠,更人性化是调用 notifyDataSetChange 虽然。

To conclude, if your design suggests that you should recreate and reassign the Adapter, you can just keep that implementation. More reliable and user friendly is to invoke notifyDataSetChange though.

样的建议

您应该思考设计的变化。

You should think about a change of design.

很不错的说,但并不总是适用(例如,一个可以工作在一个团队或维持在那里他有没有控制/资源来重新实现所有的应用程序)。

are nice to say but not always applicable (e.g. one may be working in a team or maintaining an application where he's got no control/resources to reimplement everything).