Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a spinner dynamically?

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.

typeList = dbAdapter.getList(); //array list with the values  adapter.notifyDataSetChanged(); groupSpinner.postInvalidate(); groupSpinner.setAdapter(adapter); 

The values of typeList are correct but they're not being updated in the Spinner.

like image 773
2Real Avatar asked Jul 19 '10 17:07

2Real


People also ask

How to update spinner value in android?

spinnerAdapter. setNotifyOnChange(true); From then on when you add new data it will be automatically updated. For ArrayAdapter, the default is already true.

What is spinner in mobile application development?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object.

How do I add a spinner to my toolbar?

Adding spinner to app bar/ toolbar is very simple, you just need to create a XML file in res/menu/ folder and add a item like your over flow menu and spinner widget as item actionViewClass, rest in your java code. Spinner can be added to android actionbar/toolbar with many ways.

What is custom spinner in Android?

In Android, Spinners provides you a quick way to select one value from a set of values. Android spinners are nothing but the drop-downlist seen in other programming languages. In a default state, a spinner shows its currently selected value. It provides a easy way to select a value from a list of values.


2 Answers

Actually, you either have to call clear/add on the adapter, or create and set a new adapter. The adapter does not retain a reference to your list (it is only calling toArray on your list at construction), so there is no way for it to update itself.

dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, newStringList); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerCategory.setAdapter(dataAdapter); 
like image 104
AdamC Avatar answered Sep 18 '22 11:09

AdamC


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() 
like image 38
Fifer Sheep Avatar answered Sep 16 '22 11:09

Fifer Sheep