What does GetItemIdAtPosition in MonoDroid return?

Go To StackoverFlow.com


I couldn't find any documentation for the function online, Google found nothing even remotely useful. I tried to find the original code but failed :) (I doubt I could've made sense of it anyway.) So does anyone know what this function actually does; what "Item ID" it returns?

I figured that if GetItemAtPosition returns the string contained in an item, GetItemIdAtPosition might return the content of a "name" attribute. But as usual it's not as expected.

I used a spinner based on this:

<string-array name="choices">
    <item>Choose action</item>
    <item name="3">Back to 3</item>
    <item name="2">Back to 2</item>

Using a toast for output when a choice is selected from the spinner:

private void choice_callback (object sender, ItemEventArgs e) {
    Spinner spinner = (Spinner)sender;
    string toast = string.Format ("Chosen action: {0} at pos {1} ID {2}", 
        spinner.GetItemAtPosition (e.Position), 
    Toast.MakeText (this, toast, ToastLength.Short).Show ();

Outputs "Chosen action: Back to 3 at pos 1 ID 1" and similar; in other words, the return of spinner.GetItemIdAtPosition(e.Position) seems to be the same as e.Position itself.

Side note: The app is based on this spinner tutorial: http://docs.xamarin.com/android/tutorials/User_Interface/spinner. I only adopted the bits you can see above to try and see if items in a drop-down list can be identified other than by their position.

2012-04-03 23:51
by Armatus


It returns the value that was returned by GetItemId(int position) of the adapter. Here is an example that shows how. It is based on the http://docs.xamarin.com/android/tutorials/User_Interface/spinner tutorial:

protected override void OnCreate(Bundle bundle)


    Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);

    spinner.ItemSelected += SpinnerItemSelected;
    spinner.Adapter =
        new MyAdapter(this, Android.Resource.Layout.SimpleSpinnerItem,

private void SpinnerItemSelected(object sender, ItemEventArgs e)
    Spinner spinner = (Spinner) sender;
    string toast = string.Format("The planet is {0}",
    Toast.MakeText(this, toast, ToastLength.Long).Show();

public class MyAdapter : ArrayAdapter
    private int[] _newIds = new[] {9, 7, 5, 3, 1, 8, 6, 4, 2};

    public MyAdapter(Context context, int textViewResourceId, object[] objects)
        : base(context, textViewResourceId, objects)

    public override long GetItemId(int position)
        return _newIds[position];

What happens is, when you select an item, it shows the value that was returned by the adapter. In this example I used random values for each position. If you select the second item, it returns the value in the _newIds array at the second position, which is 7.

2012-04-04 08:31
by Matthew
Thanks very much, that makes sense! Do you happen to know where the ID values normally come from? I think for now I'll have a try and see if I can use your example to extract the "name" values or something similar - Armatus 2012-04-04 09:01
If you use the ArrayAdapter.CreateFromResource( ... ) method to get the adapter, I think it uses the position as the id - Id of item at pos 0 is 0, id of item 1 is 1. I think all of the adapters do this. Maybe someone else can comment - Matthew 2012-04-04 09:04
If you look at the code for ArrayAdapter, you can see that the getItemId() method just returns the position: https://github.com/android/platformframeworksbase/blob/master/core/java/android/widget/ArrayAdapter.java#L35 - Greg Shackles 2012-04-04 14:10
@GregShackles Thanks, very useful reference. Bookmarked :- - Armatus 2012-04-06 10:28