ScottF ScottF - 1 month ago 8
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

Yes, it can be done

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

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.

Comments