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

  • Home
  • SEARCH
  • 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 9204879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:49:01+00:00 2026-06-17T23:49:01+00:00

I understand how initialize a spinner from String Array in the resources file String.xml.

  • 0

I understand how initialize a spinner from String Array in the resources file String.xml. However my issue always is how to link things properly when you select something from drop down menu. For example let say that your string array looks something like that

Electricity
Gas
Other

So in onItemSelected listener, if I want to know what was clicked to take a certain action, then should I check the pos and act on it (given i know position in my string array as below

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        switch(pos){
            case 1: // Electricity

            break;
            case 2: //Gas

            break;
                    .
                    .
                    .
        }       
    }

OR should I do Sting comparison like :

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        if(parent.getSelectedItemAtPos(pos).toString().equal("Electricity"){

            } else if (......){
            }
            .
        .
    }

The advantage of first is it is cleaner but the problem I have to make sure that when I change the resource file, I would always keep track of updating indexes.
The second is safer but it seems to be “wordy” doing String compare.

I am not, maybe I am missing an optimum solution or what I suggested above is simply ok?

Please help experts, I would rather do something right the first time as I would have many spinners in my app 🙁

Thanks

  • 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-17T23:49:03+00:00Added an answer on June 17, 2026 at 11:49 pm

    Between the two of these, without question the first option. For one, it’s a switch statement so it’s an instant jump to the right handle, whereas with the second you have a linear line of comparisons until you reach the one that you want. Secondly, you’re using String comparison in the second which is slower and less safe than just doing an integer comparison. It’s a little more work if you plan on changing it a lot, but I think it’s the better way of the two, personally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that in Objective-C you declare an array in the header file and
i found this link : initialize UITableViewController with [super initWithStyle:UITableViewStyleGrouped] but i don't understand
I'm trying to understand the array setup in java. Why must you initialize space
Can someone please help me understand this solution : Initialize 2D array with 81
So from what I understand, reserve doesn't initialize the the abstractions but simply allocates
My understand has always been that when I declare an array on the stack
I understand, from MSDN, that ClassInitialize is to mark a method that will do
I am using this: byte[] buffer = new byte[10240]; As I understand this initialize
Static members confuse me sometimes. I understand how to initialize a simple built in
I understand how to initialize a git-svn repo, create a branch, do some work,

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.