ScottF ScottF - 1 year ago 92
Android Question

Set Android spinner strings at runtime

I am trying to take an of strings and is dynamically created at runtime, and populate a spinner with them.

Is this possible, I can not find an approach to do this with in the documentation.

Answer Source

Yes, it can be done

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray);

and when the data got changed, repeat the same flow above and it'll refresh the Spinner.

A more clean solution is to create the adapter once and call mAdapter.notifyDatsetChanged() when the list gets updated. The same approach like when you're using a ListView or RecyclerView.