title: Selecting Item from Android Spinner and Using It
body: <p>I have a spinner which is populated from an Enum:</p>
  
  <pre><code>public enum Frequencies
      {
      EVERYDAY("Every day"), EVERYOTHERDAY("Every second day");
  
      private String friendlyName;
  
      private Frequencies(String friendlyName)
      {
          this.friendlyName = friendlyName;
      }
  
      @Override public String toString()
      {
          return friendlyName;
      }
      };
  </code></pre>
  
  <p>The <code>toString()</code> overrides means the spinner displays user friendly names.</p>
  
  <p>The spinner is populated like so:</p>
  
  <pre><code>        ArrayAdapter&lt;Frequencies&gt; adapter = new ArrayAdapter&lt;Frequencies&gt;(this, android.R.layout.simple_spinner_item, 
              Frequencies.values());
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      newAlarmFreqSpinner.setAdapter(adapter);
  </code></pre>
  
  <p>All of this is working well. When the user chooses a frequency from the spinner, I'd like to use the value to set a <code>long</code> value to represent the interval between alarms in a simple <code>Alarm</code> object. So the value for "Every day" would be the number of seconds in one day.</p>
  
  <p>However, I can't quite get my head around how to elegantly handle the Frequency object I retrieve from the spinner. My initial thought was to use a <code>switch</code> statement to switch on the displayed string to determine the long value I require, but of course you can't <code>switch</code> on a string.</p>
  
  <p>I then tried modifying the Frequencies enum so each entry stored its own long value:</p>
  
  <pre><code>    EVERYDAY("Every day", 86400), EVERYOTHERDAY("Every second day", 86400 * 2);
  
  private String friendlyName;
  public final int intervalInSecs;
  
  private Frequencies(String friendlyName, int intervalInSecs)
  {
      this.friendlyName = friendlyName;
      this.intervalInSecs = intervalInSecs;
  }...
  </code></pre>
  
  <p>But the switch then complains that "case expressions must be constant expressions" (even though I've made <code>intervalInSecs final</code>):</p>
  
  <pre><code>    Object obj = newAlarmFreqSpinner.getSelectedItem();
      switch(((Frequencies)obj).getIntervalInSecs())
  {
      case Frequencies.EVERYDAY.intervalInSecs: //Java doesn't like this
      {
  
          break;
      }...
  </code></pre>
  
  <p>I'm sure I'm making this more difficult than it really is! I'd appreciate any ideas.</p>