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>
</string-array>
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),
e.Position,
spinner.GetItemIdAtPosition(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.
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)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner);
spinner.ItemSelected += SpinnerItemSelected;
spinner.Adapter =
new MyAdapter(this, Android.Resource.Layout.SimpleSpinnerItem,
Resources.GetStringArray(Resource.Array.planets_array));
}
private void SpinnerItemSelected(object sender, ItemEventArgs e)
{
Spinner spinner = (Spinner) sender;
string toast = string.Format("The planet is {0}",
spinner.GetItemIdAtPosition(e.Position));
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
.
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