Sander
Sander

Reputation: 5

How to get the ID of the spinner?

I want to get the: "ID" of the selected item from a spinner.

Here is my code:

var sterren1Lems = new String[]
         {
                "1 out of 5 stars", "2 out of 5 stars", "3 out of 5 stars", "4 out of 5 stars", "5 out of 5 stars"
         };

            sterren2Lems = FindViewById<Spinner>(Resource.Id.sterrenLems);
            sterren2Lems.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem, sterren1Lems);

            TextView test = FindViewById<TextView>(Resource.Id.test);

            sterren2Lems.ItemSelected += delegate
            {
                test.Text = sterren1Lems[Convert.ToInt32(sterren2Lems.SelectedItemId)];
            };

Please help me!

Upvotes: 0

Views: 177

Answers (2)

Shree Krishna
Shree Krishna

Reputation: 8562

Either of approach in your case didn't work, I got to know by discussion that you are able to get a single string from array. So that just use simple loop to get the id.

sting yourString ="2 out of 5 stars";
string id = "";

for (int i = 0; i < yourString.Length; i++)
{
    if (Char.IsDigit(yourString[i]))
        result += str[i];
    else
        break;
}

OR

string id = new String(yourString.TakeWhile(Char.IsDigit).ToArray());

Upvotes: 0

Andres Castro
Andres Castro

Reputation: 1858

You shouldn't have to pull any value inside of the string. You know the value just by the position of the item selected.

sterren2Lems.ItemSelected += Sterren2Lems_ItemSelected;

void Sterren2Lems_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
     test.Text = sterren1Lems[e.Position];
}

Upvotes: 1

Related Questions