0

I have a spinner in which I would like to select a state in the U.S. like 'Texas', and then capture that selection. I want the display on the spinner to be "Texas", "California", "Utah", etc, but when the user selects it, and I capture the value, I want the value to be '57' instead of "Texas". I know I can just do:

String selection = my_spinner.getSelectedItem().toString();
int selectionVal;

if(selection = "Texas"){
   selectionVal = 57;
}

But I would have to do that for a lot of states. Is there a way to do it like I explained above? If not, what would be the most efficient way of doing this? Thank you.

1

5 Answers 5

3

You can create a HashMap that contains the state name as key and it's equivalent number as value.

The after the user selects a value from the Spinner, all you need is to fetch the value from the HashMap and display it.

For example, you can try something like this

HashMap<String,Integer> myMap=new HashMap<String,Integer>();

myMap.put("Texas",57);

You can add all values to this HashMap.

Now when you get the selected option from the Spinner, you can fetch the value from HashMap.

selectionVal=myMap.get(selection);

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

2

Use ONLY 3 lines of code. Save all your selection in array as following, then call them by spinner.getSelectedItemId()

String selection = my_spinner.getSelectedItem().toString();
int[] selectionVal = {57,58,59,60,61};

int sp_selected_val = selectionVal[my_spinner.getSelectedItemId()];

1 Comment

This was another valid accomplishing what I wanted. I chose another answer only because in my application's case being able to index a HashMap by state's names will be useful. Great and efficient answer!
2

In your strings xml resource:

<string-array name="state_titles">
    <item>Texas</item>
    <item>California</item>
    ...
</string-array>

<string-array name="state_values">
    <item>57</item>
    <item>58</item>
    ...
</string-array>

In your code:

String[] stateValues = getResources().getStringArray(R.array.state_values);
selectionVal = Integer.valueOf(stateValues[my_spinner.getSelectedItemPosition()]);

Comments

0

String[] str=new String[]{"one","two","three"};

ArrayAdapter yourarray= new ArrayAdapter(MyActivityClass.this,android.R.layout.simple_spinner_item, str); yourarray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapt.setAdapter(yourarray);

try this

Comments

0

you can create enum for that:

public enum State {

    Texas(57),la(59);

    private int value;

    State(int value) {
         this.value = value;
    }

   public int getValue() {
        return value;
   }

   public void setValue(int value) {
        this.value = value;
   }
}

and then in your code you can fill spinner with enum value .

ArrayAdapter<State> stateAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, State.values());
    stateSpinner.setAdapter(stateAdapter);

and get int value like this:

int selectedState = State.values() [stateSpinner.getSelectedItemPosition()].getValue();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.