title: Adding OnItemSelectedListener to Spinner
body: <p>I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.</p>
  
  <p>This is my OnCreate function</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      Button load_routes = (Button)findViewById(R.id.load_routes);
      Spinner routes = (Spinner)findViewById(R.id.routes_list);
  
      load_routes.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
              load_routes(v);
          }
      });
  
      routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
      {
          public void onItemSelected(AdapterView&lt;?&gt; arg0, View v, int position, long id)
          {
              Log.v("routes", "route selected");
          }
  
          public void onNothingSelected(AdapterView&lt;?&gt; arg0)
          {
              Log.v("routes", "nothing selected");
          }
      });
  }
  </code></pre>
  
  <p>This is my load_routes function</p>
  
  <pre><code>private void load_routes(View v)
  {
      Spinner routes = (Spinner)findViewById(R.id.routes_list);
      List&lt;String&gt; routes_list = RouteParser.get_routes();
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, routes_list);
      routes.setAdapter(adapter);
  
      TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
      list_of_routes_row.setVisibility(View.VISIBLE);
  }
  </code></pre>
  
  <p>This set up does not work. The only way I got this to work is when I setup my listener as <code>routes.setOnItemSelectedListener(this)</code> Then I implement <code>OnItemSelectedListener</code> and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!</p>