Bind Spinner (Dropdown) Android

As a beginner, It is difficult to bind the data to Spinner (Drop down). If we consider HTML, it is easy to add option values to the drop down. But In Android, we have to use Adapters to bind the data. So here I am targeting my approaches to bind to the Spinner. 

Binding static Array to the spinner

static final String[] COUNTRIES = new String[] { "India", "US", "UK",
"Sri Lanka", "Austraila", "Denmark", "Saudi" };

Spinner spCountry = (Spinner) findViewById(R.id.spCounty);

ArrayAdapter<CharSequence> adCountry = new ArrayAdapter<CharSequence>(
                this, android.R.layout.simple_spinner_dropdown_item, COUNTRIES);  

adCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
 
spCountry.setAdapter(adCountry);

Binding resource Array to the Spinner

Spinner spCities = (Spinner) findViewById(R.id.spCities);

String[] cities = getResources().getStringArray(R.array.arr_cities);

ArrayAdapter<CharSequence> adCities = new ArrayAdapter<CharSequence>(
       this, android.R.layout.simple_spinner_item, cities);

adCities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

spCities.setAdapter(adCities);

Binding resource Array to the Spinner Using ArrayAdapter.createFromResource

Spinner spLocalities = (Spinner) findViewById(R.id.spLocality);

ArrayAdapter<CharSequence> adLocalities = ArrayAdapter
   .createFromResource(this, R.array.arr_localities,android.R.layout.simple_spinner_item);

adLocalities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spLocalities.setAdapter(adLocalities);



References : download_source_code       
http://developer.android.com/resources/tutorials/views/hello-spinner.html
http://developer.android.com/reference/android/widget/ArrayAdapter.html

5 thoughts on “Bind Spinner (Dropdown) Android

Leave a Reply

Your email address will not be published.