how to add items to the spinner dynamically in android?-ThrowExceptions

Exception or error:

how to add items to the spinner dynamically in android?

How to solve:
Spinner spinner = (Spinner)findViewById(;
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,;

the above is in case of the array adapter, I believe you know how to populate values with ArrayAdapter .

How can we do this in case of the SimpleCursorAdapter, i.e if we have 2 spinners and if we select the values of one spinner (that is getting the value from SimpleCursorAdapter) depending on some criteria the other spinner should be filled with values. how can we achieve that?


By calling ArrayAdapter.add to the Spinner’s ArrayAdapter.


You can follow this way

public static void selectSpinnerItemByValue(Spinner spnr, long value){
SimpleCursorAdapter adapter = (SimpleCursorAdapter) spnr.getAdapter();
for (int position = 0; position < adapter.getCount(); position++)
    if(adapter.getItemId(position) == value)
} }

You can use the above like:

selectSpinnerItemByValue(spinnerObject, desiredValue);

you can also select by index directly like


Leave a Reply

Your email address will not be published. Required fields are marked *