I\'ve been trying to update my spinner in android dynamically but nothing I try has been working.
This is the following code I\'m using to update the spinner.
You can't directly modify the original List
then call notifyDataSetChanged()
like on other adapters, as it doesn't hold on to the original List
.
You can, however, achieve the same result using the adapter itself, like so:
spinnerAdapter.clear();
spinnerAdapter.addAll(updatedListData);
spinnerAdapter.notifyDataSetChanged(); // optional, as the dataset change should trigger this by default
Based on this answer from user392117.
edit: By default, methods that change the list like add()
and remove()
automatically call notifyDataSetChanged()
(see Android Developer Documentation for setNotifyOnChange(boolean) )
public void setNotifyOnChange (boolean notifyOnChange)
Control whether methods that change the list (add, addAll(java.util.Collection), addAll(java.lang.Object[]), insert, remove, clear, sort(java.util.Comparator)) automatically call notifyDataSetChanged. If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.
So you should not need to call notifyDataSetChanged()
every time. If you find that this is the case, you can use setNotifyOnChange(true)
spinnerAdapter.setNotifyOnChange(true); //only need to call this once
spinnerAdapter.add(Object); //no need to call notifyDataSetChanged()
Apparently after you do typeList = dbAdapter.getList()
, the variable typeList
points to a different List rather than the one you fed to the adapter originally and the adapter would be somewhat confused.
So you should use the following code:
typeList.clear();
typeList.addAll(dbAdapter.getList());
Using add/remove on your adapter and using notifyDataSetChanged() enables you not having to create new adapters over and over again.
Declare your adapter global
ArrayAdapter<Object> adapter;
When you add something to the List of Objects the adapter is attached to (Strings or whatever object you use) add an add function to the adapter and call notifyDataSetChanged:
adaper.add(Object);
adapter.notifyDataSetChanged();
and when you remove an item from the List add also:
adapter.remove(Object);
adapter.notifyDataSetChanged();
edit: By default, methods that change the list like add() and remove() automatically call notifyDataSetChanged() (see Android Developer Documentation for setNotifyOnChange(boolean) )
public void setNotifyOnChange (boolean notifyOnChange)
Control whether methods that change the list (add, addAll(java.util.Collection), addAll(java.lang.Object[]), insert, remove, clear, sort(java.util.Comparator)) automatically call notifyDataSetChanged. If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.
So you should not need to call notifyDataSetChanged() every time. If you find that this is the case you you can use setNotifyOnChange(true)
adapter.setNotifyOnChange(true); //only need to call this once
adapter.add(Object); //no need to call notifyDataSetChanged()
When you have data changed in your list and when you want to update the spinner then
Create a new object of the adapter and set that adapter to the spinner. It will work sure.
Best of luck.
Edit: Also you need to call notifyDataSetChanged() on the adapter.
After you change your data, you will need to add the following code:
typeList = dbAdapter.getList()
adapter = new ArrayAdapter<String>(v.getContext(),
android.R.layout.simple_spinner_dropdown_item,typeList);
groupSpinner.setAdapter(adapter);
When you set up the spinner adapter add
spinnerAdapter.setNotifyOnChange(true);
From then on when you add new data it will be automatically updated.