Donate

If any of the tips on this site have helped you out, consider buying me a cup of coffee. -dcPages
Android Spinner Tips PDF Print E-mail
Written by dcPages   
Article Index
Android Spinner Tips
Selecting a Spinner Item
All Pages

The Android Spinner is possibly the most versatile view element in the Android namespace. OK, so it might not be as versatile as some of the layout items, like TableLayouts and RelativeLayouts, and it might not be as functional as some of the widgets, like maps and browsers, but it's still one of my favorites. And the Android Spinner is definitely one of the shiniest implementation of what HTML programmers know as a <select> element. In a previous article, we've already covered how to populate an Android Spinner, so in this article we'll cover some of the other aspects of working with Android Spinners.

When working with an Android Spinner, there are a few things you'll need to be able to do. First and foremost, you'll need to fill that spinner with some items that the user can select. To do that, please see my previous article on populating Android Spinners. Once you've done that, there are two other tasks that become essential. The first is figuring out what the user selected and reacting to that selection. The second is selecting an item for the user - which is great when you're loading an existing item from a database and want to make sure the spinner is pointing at the right item.

To begin with, we'll take a look at figuring out what item a user has selected from your spinner. This is most easily done by using what Android calls an "OnItemSelectedListener". That does just what it sounds like - i.e. listens for a user to select something from the spinner. Let's take a look at some code for how to create an OnItemSelectedListener.

Spinner s = (Spinner) findViewById(R.id.spinner_id);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
 
@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
//do something here
}
 
@Override
public void onNothingSelected(AdapterView arg0) {
//do something else
}
});
 

The code above creates a reference to our Android Spinner by grabbing it using the id that you assigned to is in your XML file (which looks something like <Spinner android:id="@+id/spinner_id" />). Then it creates a listener by implementing the OnItemSelectedListener interface - note: even if you're not going to do anything with the "onNothingSelected" function, you still need to implement it.

Now that we know how to create a listener for our Android Spinner, we can talk about how to find out what item the user selected. To do that, our listener gives us a few hints that we can use to figure it out. First, it gives us the adapter that we used to populate the spinner. Second, it gives us the view item of the spinner they selected from - which means that you could create just one OnItemSelectedListener and then use the view they selected from to determine what you want to do about it, but for this discussion, we'll ignore this option and just use one listener for one spinner. Third, it gives us an integer of the item they selected; this is basically just the position in the array of the item, so if they selected the first item, then i = 0, if they selected the third item, then i = 2, and so on. Finally, it gives us a long value, which is the row id of the item selected.

Knowing when to use which is pretty easy to figure out, basically, if you populated the spinner with a string-array, then you can just use the integer that tells you the position in the array of the item they selected. However, if you populated the spinner with a database query, then you are better off using the long value that tells you the row id of the record in the database. If you populated the spinner manually, then you'll probably want to use a combination of the integer position and the adapter.

Let's start by taking a look at what to do when you populated your Android Spinner with a string-array. In that case, you already know what values in the array have which positions, since you created the array yourself in your strings.xml file. For example, let's say you have an array of three colors that looks like this:

<string-array>
  <item>red</item>
  <item>blue</item>
  <item>green</item>
</string-array>
 

In this case, you already know that the positions are as follows: red=0, blue=1, green=2. So if you're listener gives you and integer value of 1, then you know they selected blue.

If, however, you populated your Android Spinner from a database query, then it's hard - if not impossible - to know what position in the adapter each possible item will have. So in that case, you will need to use the long value, which tells you the row id of the item they selected. In some cases, the row id is all you need to know, but often you'll want to use that in a query to get the rest of that record.

Finally, if you populated the spinner manually, then you will probably need to use a combination of the integer position and the adapter that you used to populate the spinner. (note: you can also use this technique for spinners populated with string-arrays, which can be helpful for very large arrays where it's harder to keep track of which item goes where). To do this, we'll grab the integer value and pass that to the adapter so that it can tell us what value they selected. That code goes something like this:

adapter.getItemAtPosition(i).toString();
 

Basically, we just asked the adapter to tell us what item is at the position integer that we were given. This method actually passes us an object, which hints that we can stuff a lot more than just strings into our adapters (like an array of custom objects), but for this example, we're just assuming that you're looking for a string.

Also note that if you populated your spinner with an array that you built yourself, you could just grab the item from the array, but that would require you to declare your array as a member of your class (and not just a variable in the function where you populated your spinner). In fact, this is typically how I like to handle it when I populate a spinner manually. I just declare the array I'm going to use as a member of my class, then fill it in one function (usually the same function where I populate the spinner, but not necessarily, since I can reference it from any function in my class due its status as a member of the whole class), then I can retrieve values from it in another function (like the OnItemSelectedListener).

 



 
 

Search

Polls

How Many Phone Numbers Should dcRedial Show?
 
generic viagra