2016-05-12 99 views
0

我想从微调器中获得所选项目的“ID”。如何获取微调器的ID?

这里是我的代码:

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)]; 
      }; 

请帮帮我!

+0

一个提示首先获取文本和'substring'它。 –

+0

当我只想要所选文本的id /值时,为什么要使用子字符串? –

+0

你不愿意从''1满分5颗星''得到'1'吗? –

回答

0

无论是在你的情况的做法没有奏效,我通过讨论,你能够从阵列得到一个单一的string就知道了。因此,只需使用简单的循环即可获得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()); 
+0

但是现在我需要获取所选的字符串的值而不是1个字符串 –

+0

正如我所知道的,Andres已经告诉您从微调器中获取选定的文本。而且你也告诉你能够得到那个。 –

+0

但它没有工作,现在我需要他的代码+1,但我忘了它 –

1

您不应该在字符串内部拉取任何值。你只知道所选项目的位置。

sterren2Lems.ItemSelected += Sterren2Lems_ItemSelected; 

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

我总是得到值:“5”,因为有5个数组。 –

+0

我不确定你的意思。使用上面的代码,即使您选择第一个项目,您也可以获得5号位置? –

+0

是的,这是正确的。 –