title: Android Spinner: Get selected custom object
body: <p>In this simple example I will show you how I usually get the selected item of a spinner, which is filled with items from a custom class.</p>
  
  <pre><code>// Simple sample class
  public class StringWithIndex
  {
      public string itemString;
      public int itemIndex;
  
      public StringWithIndex(string stringPart, int indexPart)
      {
          itemString = stringPart;
          itemIndex = indexPart;
      }
  
      public override string ToString()
      {
          return itemString;
      }
  }
  
  // Create sample list
  List&lt;StringWithIndex&gt; spinnerItems = new List&lt;StringWithIndex&gt;();
  spinnerItems.Add(new StringWithIndex("A", 1));
  spinnerItems.Add(new StringWithIndex("B", 2));
  spinnerItems.Add(new StringWithIndex("C", 3));
  
  // Create Spinner
  Spinner spinner = new Spinner(Context);
  var spinnerAdapter = new ArrayAdapter&lt;StringWithIndex&gt;(Context, Android.Resource.Layout.SimpleSpinnerItem, spinnerItems);
  spinner.Adapter = spinnerAdapter;
  
  // Spinner Item selected
   spinner.ItemSelected += (sender, e) =&gt;
  {
      var pos = spinner.SelectedItemPosition;
      var item = spinnerItems[pos];
      // do something with item
  };
  </code></pre>
  
  <p>I know the selected item position and search in the list which is used for the adapter for the selected item.</p>
  
  <p>So my question is, is there a way to access the object directly, without searching in the original list?
  For example a cast of the selected item does not work</p>
  
  <pre><code>var itemString = (spinner.SelectedItem as StringWithIndex).itemString;
  </code></pre>
  
  <p>I do not know the benefits of accessing the item without the list right now, but everytime I am implementing a spinner I ask myself this question.</p>
  
  <p><strong>EDIT</strong>:
  My thoughts are heading to use SelectedItem. I know there many ways to use the position in any kind of passed list or overriden method from the adapter.</p>