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?
notifyDataSetChanged(). notifyDataSetChanged will work for editing or modifying data set, your null pointer is because of something else, check the logs.
notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
A bit late, but I didn't like Ragnar's response, because it doesn't explain everything.
Basically,
myListAdapter.notifyDataSetChanged()
vs
setListAdapter(new ArrayAdapter(myList));
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.
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.
Suggestions of kind
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).
due to my design
You should think about a change of design.
How much cost ( in terms of execution time) such decision will add compared to using notifidatasetChanged?
I don't like talking about "how much memory", "memory leaks" etc. but please, imagine that situation as "normal person".
You can image your goal with a building new house. You once builded house. Everything is ok but later, you want some change! So you're going to make some changes for example change colors, replace windows with new ones, join to house some shelter etc. and what you'll do?
You'll demolish the house? Or you'll make only changes?
I think that answer you already know. Why you don't want to demolish the house but only make changes?
Same is with destroying and assigning new adapter. It's waste of time, maximal inefficient and "not comfortable at least for me".
Make changes in adapter (like add new items, update old, change row colors etc.) is much more efficient, clean and faster. API already provides you method how you can achieve it.
I think your idea of design is not correct and efficient and you should review what you'll do finally. Try to think about man. Hope this answer will make things more clear for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With