Android ApiDemos example resolution (192): Views-& gt; Spinner

Source: Internet
Author: User

The Spinner is a drop-down box and a subclass of AdapterView. The display of AdapterView can be achieved through data binding. The data source can be an array or database record, and the data source and AdapterView can be a bridge through Adapter. Using the Adapter, AdatperView can display data sources or process user selection events, such as selecting an item in the list.

In this example, the Adapter uses the ArrayAdapter. For details, see the Android ApiDemos example resolution (168): Views-> Lists-> 1. Array

Add event processing for the Spinner:


[Java] view plaincopyprint?
Spinner s1 = (Spinner) findViewById (R. id. spinner1 );
ArrayAdapter <CharSequence> adapter
= ArrayAdapter. createFromResource (
This, R. array. colors,
Android. R. layout. simple_spinner_item );
Adapter. setDropDownViewResource (android. R
. Layout. simple_spinner_dropdown_item );
S1.setAdapter (adapter );
S1.setOnItemSelectedListener (
New OnItemSelectedListener (){
Public void onItemSelected (
AdapterView <?> Parent,
View view, int position, long id ){
ShowToast ("Spinner1: position ="
+ Position + "id =" + id );
}
 
Public void onNothingSelected (AdapterView <?> Parent ){
ShowToast ("Spinner1: unselected ");
}
});

Spinner s1 = (Spinner) findViewById (R. id. spinner1 );
ArrayAdapter <CharSequence> adapter
= ArrayAdapter. createFromResource (
This, R. array. colors,
Android. R. layout. simple_spinner_item );
Adapter. setDropDownViewResource (android. R
. Layout. simple_spinner_dropdown_item );
S1.setAdapter (adapter );
S1.setOnItemSelectedListener (
New OnItemSelectedListener (){
Public void onItemSelected (
AdapterView <?> Parent,
View view, int position, long id ){
ShowToast ("Spinner1: position ="
+ Position + "id =" + id );
}

Public void onNothingSelected (AdapterView <?> Parent ){
ShowToast ("Spinner1: unselected ");
}
});

 

 


 

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.