title: Example: How To Programmatically Fill An Android Spinner from an Array in MonoDroid / C#
body: <p>I started fiddling around with MonoDroid this week. Not that I don't appreciate Java, but I spend most of my time in either Visual Studio / C# or Delphi, and I'd rather pull my fingernails out than learn YET ANOTHER IDE and YET ANOTHER editor in order to use Eclipse. </p>
  
  <p>I adapted this code to C# from Java, for situations where it's easier to populate my spinner programmatically in C# code rather than in the AXML. Perhaps some of you may find it helpful.</p>
  
  <p>Create a generic Spinner In Your Activity's axml:</p>
  
  <pre><code>&lt;Spinner
      android:id="@+id/spinner"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:prompt="@string/spinner_prompt"
  /&gt;
  </code></pre>
  
  <p>Build your array of string somewhere appropriate:</p>
  
  <pre><code>public static readonly string[] refresh_ratesS = {"10 seconds","20 seconds","30 seconds","60 seconds","5 minutes"}; 
  </code></pre>
  
  <p>I prefer to instantiate objects in the Activity's public class so that I only have to identify them once:</p>
  
  <pre><code>public class ActivityChooseRefreshRate :Activity {
      Spinner         jb_spinner;
  </code></pre>
  
  <p>In the Activity's OnCreate function assign your array to the spinner:</p>
  
  <pre><code>     //identify the spinner
  jb_spinner      =   FindViewById&lt;Spinner&gt;(Resource.Id.spinner);
       //create the handler for when the user selects something
  jb_spinner.ItemSelected += new EventHandler&lt;ItemEventArgs&gt; (jb_spinner_ItemSelected);
      //assign your array to an ArrayAdapter
  ArrayAdapter&lt;string&gt;  spinnerArrayAdapter   =   new ArrayAdapter&lt;string&gt;(this,Android.Resource.Layout.SimpleSpinnerItem,refresh_ratesS);
        //Use the ArrayAdapter you've set up to populate your spinner
  spinnerArrayAdapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerItem);
  jb_spinner.Adapter      =   spinnerArrayAdapter;
     //optionally pre-set jb_spinner to an index
  jb_spinner.SetSelection(2);
  </code></pre>
  
  <p>Lastly, code the jb_spinner_ItemSelected handler so that something happens when the user makes a selection:</p>
  
  <pre><code>private void jb_spinner_ItemSelected (object sender, ItemEventArgs e){
      Spinner spinner     = (Spinner)sender;
           //do something here, such as raise a Toast
      Toast.MakeText(this, "Index="+e.Position.ToString(),ToastLength.Long).Show();
  }
  </code></pre>