如何在没有侦听器的情况下从Spinner获取当前项目-c#android xamarin
我试图获取当前在微调器中的项目,但不是通过Selected或OnChange事件如何在没有侦听器的情况下从Spinner获取当前项目-c#android xamarin,c#,android,xamarin,xamarin.android,spinner,C#,Android,Xamarin,Xamarin.android,Spinner,我试图获取当前在微调器中的项目,但不是通过Selected或OnChange事件 public string GetCurrentSport() { string currentSport = spnSports.GetItemAtPosition(0).ToString(); return currentSport; } 上面的代码抛出并出错,我猜获取微调器值的代码是错误的,这是通过单击按钮传递给另一个类的,
public string GetCurrentSport()
{
string currentSport = spnSports.GetItemAtPosition(0).ToString();
return currentSport;
}
上面的代码抛出并出错,我猜获取微调器值的代码是错误的,这是通过单击按钮传递给另一个类的,因此我不能使用OnSelected事件
如果您需要更多的代码,请询问。一个
微调器通过其子类AdapterView
有三种方法来获取当前选定的“项目”SelectedItem
,SelectedItemId
,SelectedItemPosition
:
// 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:在OnCreate中,我将此代码放入:string selected=spnsport.SelectedItemId.ToString()
并在类级别给出一个Null异常错误:Spinner spnsport:spnsport=findviewbyd(Resource.Id.spnSportCategory)代码>在这之前一切都正常,因为我正在使用适配器加载微调器值,但当我运行上述代码时,它只会给我nullsaying@DEFALT如果当前未选择任何内容,则可以为null
:与当前所选项目对应的数据,如果未选择任何内容,则为空。