Skip to content

Retrieving Spinner Values

January 13, 2010

Hey everyone,

So recently I’ve seen a lot of people with questions on retrieving values from Spinners. Now, it’s well known that you can set the Spinner position by index, but the big question is:

“If I don’t know the index of the value I want, how would I set the spinner to that value (for instance by default)?”

Well this is just going to be a quick post for people who are interested on how you would do so. As a warning it’s not an elegant solution (actually it’s pretty much brute force) but the main point of this example is to give developers some piece in mind that NO KNOWN ELEGANT SOLUTION EXISTS. So for those who are rigorously searching through documentations and looking online hoping to find a method, you won’t find one and so you’re going to have to live with this little code snippet:


public int getIndexFromElement(ArrayAdapter adapter, String element) { 
      for(int i = 0; i < adapter.size(); i++) { 
            if(adapter.getItem(i).equals(element)) { 
                  return i; 
            } 
      } 
      return 0; 
}

In the example I assume the “value” you want to search for is a String, but again it can be anything you want (depending on what kinds of values your spinner contains… but yes usually Strings).

So again, pretty short post and nothing too complicated. Just thought it’d be a nice way to hopefully save people some time searching for a method that doesn’t exist.

– jwei

Update

I haven’t tested this out myself but I think this much shorter snippet also accomplishes the same thing:

// value is equivalent to "element" above
spinner.setSelection(adapter.getPosition(value)));

A thanks to Doug for spotting this shortcut.

7 Comments leave one →
  1. Doug Campbell permalink
    March 24, 2010 5:02 pm

    I think you can do what you are describing above with the adapter’s getPosition() method. I’m using a line of code like this in my app (and it appears to be working):

    spinner.setSelection(adapter.getPosition(value)));

    effectively finding the position of a given value (passed as a string) and selecting that result with the setSelection() method.

    Hope that helps,
    Doug

  2. Doug Campbell permalink
    March 24, 2010 5:08 pm

    oops, that code has an extra ) at the end, it should read:

    s.setSelection(adapter.getPosition(value));

    That extra paren came from me calling a toString() method, because getPosition wants a String, but my DB is returning a float, so my actual code looks more like this:

    s.setSelection(adapter.getPosition(Float.toString(floatValue)));

    Sorry for any confusion,
    Doug

    • April 5, 2010 5:22 pm

      Hey Doug!

      Sorry for taking so long in getting back to you.

      Yea I definitely think what you suggest works! And is much nicer than what I suggested so I’ll make sure I make an addendum to my post.

      The cool thing, though, is if you think about how:

      adapter.getPosition(value)

      is actually implemented, then it probably looks something like code snippet that I gave. In other words, the getPosition() method for the Adapter class is probably an O(n) call that loops through all of the adapter’s values until it finds the index of the value it wants, very similar to what I do in my example.

      But yes just something neat to think about. Thanks again Doug =D

      – jwei

    • Joshua Sigar permalink
      July 8, 2010 11:27 am

      I was excited but turned out getPosition() is only available for ArrayAdapter. For SimpleCursorAdapter, you still have to use the solution in the original post.

  3. manuel permalink
    September 17, 2010 7:42 am

    Hello, thanks for the tutorial. but it seems there is not a .size() method on an array adapter, as you did . how did you get that method. i can use the other method by just passing in the position of the string but it won’t be efficient for a long list.

  4. October 30, 2010 6:36 am

    You can find an article that demonstrates populating a spinner control with an array of java objects that can contain much more than the displayed text at http://www.katr.com/article_android_spinner01.php. It shows how to create and populate the control, then how to extract the object in the onItemSelected event.

  5. sunil permalink
    November 22, 2011 10:18 pm

    I found this useful. While using android:entries to provide data to spinners, I wanted to know the position of an element in the array. Had to read the arrays into a List and indexOf to find the element position.

Leave a comment