An example of using the spinner drop down list in the anroid development tutorial

  • 2020-06-01 10:58:13
  • OfStack

First, add the Spinner control to the xml file:


Write in Activity:

package com.example.spinner;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {

 private Spinner spinner;
 protected void onCreate(Bundle savedInstanceState) {

  spinner = (Spinner)this.findViewById(;
  // Set data source 
  String[] mydata = {" Beijing ", " Shanghai "," guangdong "," guangxi "};

  // The statement 1 a ArrayAdapter And associate the data source with it 
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mydata);
  // Sets the style of the pop-up drop-down list 
  // will arrayAdapter Object add in Spinner Go to the 
  // Add listener 
  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
   public void onItemSelected(AdapterView<?> arg0, View arg1,
     int arg2, long arg3) {
    Toast.makeText(MainActivity.this, " Select the: " + arg2, Toast.LENGTH_SHORT).show();
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub


Related articles: