SPINNER XAMARIN.ANDROID

Viewed 984

Im looking for a spinnerexample that include key/value like DisplayMember/ValueMember like Winforms. im Usign Xmarin.Android in Visual Studio 2017/2015, i being use this to populate, but it only contains MembersValue

<string-array>
    <ítem>ITEM 1</ítem>
</string-array>

and i looking for some like:

<string-array>
<ítem value=1>ITEM 1</ítem>
</string-array>

where i can get the tag valuein the method click from a buttoncontrol like this:

private void btnButton(Object sender,EventArgs e)

{
   string strValue=spinner.SelectedItem.ToString();
}

Actual Result : ITEM 1

Expected Result: 1

I was trying with a dictionary as show below:

var dict = new Dictionary<int, string>();
        dict.Add(1, "Tarjeta1");
        dict.Add(2, "tarjeta2");

and then Add as resource in the spinner:

spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(Spinner1_ItemSelected);    
ArrayAdapter adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleSpinnerItem,dict.Values.ToArray());
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;

But when i trying to get the values, it displays: Tarjeta1 or Tarjeta2 instead 1 or 2

I hope anyone can support me.

Best regards.

1 Answers

You can use baseAdapter, watch video tutorial https://www.youtube.com/watch?v=4o5R4qvJXZM

Or simply if your spinner items text restricted to prefix or suffix words you can extract the numeric value you want from it

private void btnButton(Object sender,EventArgs e)
{
   int intValue = Convert.ToInt32(spinner.SelectedItem.ToString().Replace("Tarjeta", string.Empty).Trim());
   string strValue = intValue.ToString();
}
Related