简体   繁体   中英

select item selected in android spinner

I am creating a spinner for android and I have it set up in the following way:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

spinner =(Spinner) findViewById(R.id.spinner1);

ArrayAdapter <String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, OTT);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(this);
}

and I have a listener as follows:

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,


long arg3) {

}

What I want to happen is for a string to take the value of the item the user selects form the spinner

To get the value of the item selected by the user you would add the following line to your listener:

String item = spinner.getSelectedItem().toString();

I develop with Xamarin.Android, so my code is in c#, but it's fairly similar in allot of ways. One of the arguments the event should return is the position of the selected spinner item. With that you can invoke the function getItemAtPosition and it will return the correct value!

((Spinner)sender).GetItemAtPosition (e.Position).ToString ()

So my guesses are, it translates to something like this in java

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
 arg0.getItemAtPosition(arg2).toString()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM