English 中文(简体)
How to get the selected item of Android Spinner?
原标题:

How to get a selected item from a spinner in android?

问题回答
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

Note:

The documentation for the Spinner widget clearly states:

A spinner does not support item click events. Calling this 
method will raise an exception.

Better use OnItemSelectedListener() instead, as above.

In your Activity:

mSpinner = (Spinner) findViewById(R.id.my_spinner);

// Define your adapter, in my case I have a class (SpinnerData) so I get an ID instead of the spinner s text 
ArrayAdapter<SpinnerData> myadapter = new ArrayAdapter<SpinnerData>(
  this, android.R.layout.simple_spinner_item, new SpinnerData [] {
  new SpinnerData("1","Item 1"),
  new SpinnerData("2","Item 2")
});

myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mSpinner.setAdapter(myadapter);
    mSpinner.setOnItemSelectedListener(this);

This is useful if you have other elements in the form that trigger the onItemSelected event:

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id{
    SpinnerData sd;


    switch (parent.getId()) {

    case R.id.my_spinner:
        sd = (SpinnerData)mSpinner.getSelectedItem();
        // do something with sd.id
        break;

... }

As reference, the SpinnerData class:

public class SpinnerData {

public String id;
public String name;

public SpinnerData(String _id, String _name) {

    this.id = _id;
    this.name = _name;

}

public String toString() {
    return this.name;
}

}

Hope it helps.

Try this Code..

sp2.setOnItemSelectedListener(new OnItemSelectedListener()
 {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long id) {
        // TODO Auto-generated method stub
        int selected_item_position = arg2;
        String selected_item=sp2.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

        }
 });




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签