Usage of the Spinner in Android (usage of the drop-down list)
Step 2: Write the Spinner drop-down list
To write a Spinner:
1. Set the data source. Use List set to write
2. Create an ArrayAdapter (array adapter)
3. Set a drop-down list Style
4. bind an adapter to a Spinner
5. Set the listener OnItemSelectedListener ()
Use the first one of the two types of Spinner drop-down lists:
Main function:
Public class MainActivity extends AppCompatActivity implements AdapterView. OnItemSelectedListener {private TextView mTextView; private Spinner mSpinner; private List
List; private ArrayAdapter
Adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTextView = (TextView) findViewById (R. id. textview); mSpinner = (Spinner) findViewById (R. id. spinner); // 1. Set the data source list = new ArrayList <> (); list. add (Beijing); list. add (Shanghai); list. add (Qingdao); list. add (Shenzhen); list. add (Chengdu); // create an ArrayAdapter (array adapter) adapter = new ArrayAdapter
(This, android. r. layout. simple_spinner_item, list); // set a drop-down list style adapter for the adapter. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); // loads the adapter mSpinner. setAdapter (adapter); // sets the mSpinner listener. setOnItemSelectedListener (this);} @ Override public void onItemSelected (AdapterView
Parent, View view, int position, long id) {// obtain the name of the current city // String cityName = list. get (position); String city = adapter. getItem (position); mTextView. setText (the city you select + city);} @ Override public void onNothingSelected (AdapterView
Parent ){}}
Layout file:
Second:
Main function:
Public class MainActivity extends AppCompatActivity {private TextView mTextView; private Spinner mSpinner; private SimpleAdapter adapter; private List
> Datalist; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mTextView = (TextView) findViewById (R. id. textview_main); mSpinner = (Spinner) findViewById (R. id. spinner); // Step 1: Add a list of items in the drop-down list. The items added here are the menu items in the drop-down list datalist = new ArrayList <> (); // Step 2: define an adapter for the drop-down list. The previous list adapter = new SimpleAdapter (this, getData (), R. layout. item, new String [] {image, text}, new int [] {R. id. image, R. id. textview}); // Step 3: Set the menu style adapter in the drop-down list for the adapter. setDropDownViewResource (R. layout. item); // Add the adapter to the mSpinner drop-down list. setAdapter (adapter); // Step 5: Set events for the drop-down list. mSpinner is selected in the Event Response menu. setOnItemSelectedListener (new AdapterView. onItemSelectedListener () {@ Override public void onItemSelected (AdapterView
Parent, View view, int position, long id) {mTextView. setText (you selected + adapter. getItem (position);} @ Override public void onNothingSelected (AdapterView
Parent) {mTextView. setText (NONE) ;}} private List
> GetData () {Map
Map = new HashMap <> (); map. put (image, R. mipmap. ic_launcher); map. put (text, Beijing); Map
Map2 = new HashMap <> (); map2.put (image, R. mipmap. ic_launcher); map2.put (text, Chengdu); Map
Map3 = new HashMap <> (); map3.put (image, R. mipmap. ic_launcher); map3.put (text, Shenzhen); Map
Map4 = new HashMap <> (); map4.put (image, R. mipmap. ic_launcher); map4.put (text, Qingdao); Map
Map5 = new HashMap <> (); map5.put (image, R. mipmap. ic_launcher); map5.put (text, Jinan); Map
Map6 = new HashMap <> (); map6.put (image, R. mipmap. ic_launcher); map6.put (text, Hong Kong); datalist. add (map); datalist. add (map2); datalist. add (map3); datalist. add (map4); datalist. add (map5); datalist. add (map6); return datalist ;}}