Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notifyDataSetChanged vs setAdapter

Tags:

android

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?

like image 312
Yarh Avatar asked Nov 30 '13 14:11

Yarh


People also ask

What is the function of notifyDataSetChanged?

notifyDataSetChanged(). notifyDataSetChanged will work for editing or modifying data set, your null pointer is because of something else, check the logs.

What is notifyDataSetChanged in Android?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.


2 Answers

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).

like image 83
Ilya Saunkin Avatar answered Nov 09 '22 01:11

Ilya Saunkin


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?

  • It'll cost too much to build new house (if you're not Bill Gates)
  • It's waste of time (changes will be made faster and they cost less)

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.

like image 43
Simon Dorociak Avatar answered Nov 09 '22 00:11

Simon Dorociak