DEFALT
DEFALT

Reputation: 149

How to get current item from Spinner without listener - c# android xamarin

I am trying to get the item that is currently in the spinner, but not by Selected or OnChange events.

 public string GetCurrentSport()
        {

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

the above code throws and error, I am guessing it is the wrong code to get the Spinner value, this is getting passed to another class from a button click so i can't use the OnSelected Events.

If you need more code, please ask.

Upvotes: 0

Views: 887

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

A Spinner via its subclassed AdapterView has three methods to obtain the "item" that is currently selected, 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: https://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItem()

Upvotes: 1

Related Questions