1

I developing an android application which is in C# in that application I have a spinner. I do not know how to get items from spinner. can anyone please tell me how can I get the item from spinner. Thankx in advance.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
prachi
  • 21
  • 1
  • 7

2 Answers2

1

If it is really C# you are talking about, then you probably mean a ComboBox?

enter image description here

This is how to get the value in a short example:

DataTable dataTable = new DataTable("Country");
dataTable.Columns.Add("Id");
dataTable.Columns.Add("Name");
dataTable.Rows.Add(45, "Denmark");
dataTable.Rows.Add(63, "Philippines");

comboBox1.DataSource = dataTable;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";

comboBox1.SelectedIndex = 1;
comboBox1.Refresh();

DataRow selectedDataRow = ((DataRowView)comboBox1.SelectedItem).Row;
int countryId = Convert.ToInt32(selectedDataRow["Id"]);
string countryName = selectedDataRow["Name"].ToString();

int selectedValue = Convert.ToInt32(comboBox1.SelectedValue);

Taken from here: http://social.msdn.microsoft.com/Forums/en-US/b14cf4d7-025e-459c-ac41-1e503fcdcc99/how-to-retrieve-value-from-combobox-in-c

For Android-Spinner, this is how:

Use the Spinner.getSelectedItem() method to get the currently selected item:

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
String Text = mySpinner.getSelectedItem().toString();

In this case, the Spinner was filled with Strings.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
0

You can use:

Spinner spinner= (Spinner) findViewById(R.id.your_spinner);
String item = (String) spinner.getItemAtPosition(spinner.getSelectedItemPosition());

It will get the item as a string of the selected position of a spinner.You can also find that item as an object,if that is the requirement for you then see:

Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60