Thursday, March 29, 2012

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

6 comments:

  1. Thank you so much for sharing this worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.

    Android Online Training

    ReplyDelete
  2. Not only do variations in hardware and software affect the overall compatibility of the app, it reflects poorly on the build quality of the app.gerald winata gozali

    ReplyDelete
  3. Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read.
    click here to continue your registration
    click here to confirm
    click here download
    click here digital
    click here download cb background

    ReplyDelete
  4.  I just stumbled upon your weblog and wanted to publicize that i've without a doubt enjoyed browsing your weblog posts. 먹튀

    ReplyDelete