2017-12-07 67 views
0

現在、スピンナーにあるアイテムを取得しようとしていますが、SelectedイベントまたはOnChangeイベントでは取得しようとしていません。リスナーなしでSpinnerから現在のアイテムを取得する方法 - C#android xamarin

public string GetCurrentSport() 
     { 

      string currentSport = spnSports.GetItemAtPosition(0).ToString(); 
      return currentSport; 
     } 

上記のコードはスローし、エラー、私はスピナーの値を取得するには、間違ったコードで推測しています私はOnSelectedイベントを使用することはできませんので、これはボタンのクリックから別のクラスに渡さなっています。

もっとコードが必要な場合は、お尋ねください。そのAdapterViewサブクラス化を経由して

答えて

0

Spinnerは、現在選択されている "項目"、SelectedItemSelectedItemIdを取得するには、3つの方法、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 

再:https://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItem()

+0

これは私、それを助けることはありません私のスピナーがまだヌルだと言います... – DEFALT

+0

私はOnCreateでこのコードを書いています: 'string selected = spnSports.SelectedItemId.ToString()'と 'Null Exception Error'を返します – DEFALT

+0

はい、私は言った私はOnCreateで宣言しますクラスレベルで法.... – DEFALT

関連する問題