我正在尝试获取当前在微调器中的项目,但不是通过Selected或OnChange事件。
public string GetCurrentSport()
{
string currentSport = spnSports.GetItemAtPosition(0).ToString();
return currentSport;
}
上面的代码抛出错误,我猜这是获取Spinner值的错误代码,这是从按钮单击传递到另一个类,所以我不能使用OnSelected事件。
如果您需要更多代码,请询问。
答案 0 :(得分:1)
Spinner
通过其子类AdapterView
有三种方法可以获取当前所选的“项目”,SelectedItem
,SelectedItemId
,SelectedItemPosition
:< / p>
// The data corresponding to the currently selected item, or null if there is nothing selected.
var javaObj = spinner.SelectedItem; // getSelectedItem
//The id corresponding to the currently selected item, or INVALID_ROW_ID if nothing is selected.
var id = spinner.SelectedItemId; // getSelectedItemId
// Return the position of the currently selected item within the adapter's data set
var postion = spinner.SelectedItemPosition; // getSelectedItemPosition
re:https://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItem()