Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:07:34+00:00 2026-06-04T13:07:34+00:00

I have a spinner which is populated from an Enum: public enum Frequencies {

  • 0

I have a spinner which is populated from an Enum:

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;
    }
    };

The toString() overrides means the spinner displays user friendly names.

The spinner is populated like so:

        ArrayAdapter<Frequencies> adapter = new ArrayAdapter<Frequencies>(this, android.R.layout.simple_spinner_item, 
            Frequencies.values());
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    newAlarmFreqSpinner.setAdapter(adapter);

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 long value to represent the interval between alarms in a simple Alarm object. So the value for “Every day” would be the number of seconds in one day.

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 switch statement to switch on the displayed string to determine the long value I require, but of course you can’t switch on a string.

I then tried modifying the Frequencies enum so each entry stored its own long value:

    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;
}...

But the switch then complains that “case expressions must be constant expressions” (even though I’ve made intervalInSecs final):

    Object obj = newAlarmFreqSpinner.getSelectedItem();
    switch(((Frequencies)obj).getIntervalInSecs())
{
    case Frequencies.EVERYDAY.intervalInSecs: //Java doesn't like this
    {

        break;
    }...

I’m sure I’m making this more difficult than it really is! I’d appreciate any ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:07:35+00:00Added an answer on June 4, 2026 at 1:07 pm

    Well basically the enum type has it’s associated interval. So you don’t need to switch on the interval, just switch on the enum type:

    Object obj = newAlarmFreqSpinner.getSelectedItem();
    switch((Frequencies)obj) {
    case Frequencies.EVERYDAY {
        alarm.setInterval(EVERYDAY.getIntervalInSecs());
        break;
    }...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, at the moment I have a spinner which is populated from a database
i have an array which is populated in the spinner adapter. now i wanna
I have an application, which has a Spinner that I want populated with some
Hi i have an android spinner populated from and array that i want to
I have one spinner in which few values are there from strings.xml and I
Hello I have question about android spinner I have spinner which is populated by
Hello I have question about android spinner I have spinner which is populated by
I am developing an android app in which I have 3 spinners populated from
Hi I have a spinner for which I would like to change its entry.
I have created an application for ContactsContract... I have created a spinner which brings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.