Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - setListAdapter() + notifyDataSetChanged() clarification

I have a ListFragment whose data is populated by a custom adapter ( a SimpleAdapter in my case). I was experiencing issues with using notifyDataSetChanged() from within my class that extended ListFragment. After a lot of looking around and several (useful) Stack Overflow posts later:

listview not updating with notifydatasetchanged() call

Android ListView not refreshing after notifyDataSetChanged

adapters notifyDataSetChanged does not work

notifyDataSetChanged not working

ListView does not update when calling notifyDataSetChanged from a BaseAdapter

I understand that a loose (and highly un-recommended) workaround would be to re-set your adapter using setListAdapter(). However I am now facing issues with this as well.

The documentation, http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter), mentions that setListAdapter()

Provides the cursor for the list view.

But I still have some questions.

Q1. Does initializing an adapter multiple times using setListAdapter() 'point' to the same adapter instance ?

Q2. What actually happens when a call is made to getListAdapter() and then to notifyDataSetChanged() when an adapter has been set multiple times using setListAdapter() ?

Q3. This question is based on an assumption from Q2- when notifyDataSetChanged() is called when an adapter is set multiple times, which of those adapter instances (this part is the assumption), if they exist' is actually being notified for change ?

I am a beginner with Android and I believe there a quite a few nuances I do not understand.I would be extremely grateful if you could clarify these questions. Also thank you very much for your time.

like image 625
user1841702 Avatar asked Nov 11 '22 05:11

user1841702


1 Answers

Q1. Does initializing an adapter multiple times using setListAdapter() 'point' to the same adapter instance ?

Ans: Initializing the adapter will point only to the last instance that you set using setListAdapter.

Q2. What actually happens when a call is made to getListAdapter() and then to 
notifyDataSetChanged() when an adapter has been set multiple times using 
setListAdapter() ?

Ans: It doesn't matter how many adapters that you have initialized, only the last instance will be retrieved using the getListAdapter().When you use notifyDataSetChanged() only the last instance wich is retrieved using getListAdapter() will be refreshed i.e. ; the last instance will be reloaded(By calling the getView).

Q3. This question is based on an assumption from Q2- when notifyDataSetChanged() is 
called when an adapter is set multiple times, which of those adapter instances (this 
part is the assumption), if they exist' is actually being notified for change ?

Ans: The above answer contains the explanation for this.

like image 180
Viswanath Lekshmanan Avatar answered Nov 15 '22 06:11

Viswanath Lekshmanan