Learning notes for Android: Use Cases in the Spinner drop-down list

Source: Internet
Author: User

Learning notes for Android: Use Cases in the Spinner drop-down list

 

(1) Two methods are used to extract the data source from the Spinner: using the list set or configuring through the xml file

(2) The layout code is as follows:

 

 
  
   
    
      
     
    
   
  
 

(2)
Package com. example. spinners; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. view. menu; 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;/*** use the OnItemSelectedListener interface to implement the selected event * @ author xuliugen **/public class MainActivity extends Activity implements OnItemSelectedListener {private Spinner spinner1; private Spinner spinner2; // private SpinnerAdapter spinnerAdapter; private ArrayAdapter
 
  
Adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); spinner1 = (Spinner) this. findViewById (R. id. spinner1); spinner2 = (Spinner) this. findViewById (R. id. spinner2); // the first type of manually input listadapter = new ArrayAdapter
  
   
(MainActivity. this, android. r. layout. simple_spinner_item, getDataSource (); spinner1.setAdapter (adapter); // register the listener spinner1.setOnItemSelectedListener (this); // Method 2: ArrayAdapter adapter2 = ArrayAdapter by loading the data source configured in the xml file. createFromResource (this, R. array. planets_array, android. r. layout. simple_spinner_item); adapter2.setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); spinner2.setAdapter (adapter2);}/** use the first method: manually input list */public List
   
    
GetDataSource () {List
    
     
List = new ArrayList
     
      
(); List. add (beijing); list. add (beida); list. add (shahai); list. add (shangjing); list. add (shanglian); list. add (chengdu); return list;} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true;} @ Overridepublic void onItemSelected (AdapterView
      Parent, View view, int position, long id) {String itemString = spinner1.getItemAtPosition (position ). toString (); Toast. makeText (this, you selected + itemString, 1 ). show () ;}@ Overridepublic void onNothingSelected (AdapterView
      Parent ){}}
     
    
   
  
 

 

(4) The configuration in the resource file is as follows:

 

 
     
  
   Spinners
      
  
   Settings
      
  
   Hello world!
      
          
   
    Mercury
           
   
    Venus
           
   
    Earth
           
   
    Mars
           
   
    Jupiter
           
   
    Saturn
           
   
    Uranus
           
   
    Neptune
       
  
 

(5) The running result is as follows:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.