In the development of Android projects, a large number of spinner controls are used. Next we will discuss the knowledge of the spinner. In case you forget to come and check it later. This section also describes how to use arrayadatper.
For example, I will describe in detail when leaving the company:
In layout, the content of the Main. xml file is:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Spinner
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/Spinner"
Android: text = "Spinner"
/>
</Linearlayout>
In layout, the content of the string. xml file is:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello world, spinneractivity! </String>
<String name = "app_name"> spinnertest </string>
<String-array name = "Spinner">
<Item> A </item>
<Item> x </item>
<Item> d </item>
<Item> C </item>
<Item> v </item>
<Item> F </item>
</String-array>
</Resources>
The Java code is as follows:
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. spinner;
Import Android. widget. Toast;
Public class spinneractivity extends activity {
Spinner spinner = NULL;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Spinner = (spinner) findviewbyid (R. Id. spinner );
/**
* Create an arrayadapter object using the createfromresource () method
* The first parameter is the context object.
* The second parameter references the array in the string. xml file,
* The third parameter is used to specify the spinner style. It is a layout ID provided by the Android system. You can also replace the custom Layout file.
*/
Arrayadapter <charsequence> adapter = arrayadapter. createfromresource (
This, R. array. spinner, Android. R. layout. simple_spinner_item );
// Set the display style of the spinner and reference a style provided by the Android system. The system comes with many styles, large
// You can select your preferred style or custom style. You can also customize the style as needed.
Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
Spinner. setadapter (adapter );
Spinner. setprompt ("test ");
// Set the listener of the spinner Control
Spinner. setonitemselectedlistener (New adapterview. onitemselectedlistener (){
// This method is called when the user selects an entry
/**
* First parameter: indicates that the entire list page contains a list of all entries.
* Second parameter: View of the selected entry
* Third parameter: Location
* Fourth parameter: Space ID
*/
@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
/**
* Getitematposition ()
* Get the specified location in the relevant data list
*/
Toast. maketext (parent. getcontext (), "the planet is" + parent. getitematposition (position). tostring (),
Toast. length_long). Show ();
}
@ Override
Public void onnothingselected (adapterview <?> Parent ){
}
});
}
}
In this way, you can control a spinner control. In the preceding example, arrayadapter is very limited and it is difficult to dynamically add data. The following example shows how to define a layout file and add data with a set.
Add a layout file item. XML in layout:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/textview"
/>
</Linearlayout>
Others are the same as the above example; read the Java code:
Import java. util. arraylist;
Import java. util. List;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. spinner;
Import Android. widget. Toast;
Public class spinneractivity extends activity {
Spinner spinner = NULL;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Spinner = (spinner) findviewbyid (R. Id. spinner );
/**
* Create an arrayadapter object using the createfromresource () method
* The first parameter is the context object.
* The second parameter references the array in the string. xml file,
* The third parameter is used to specify the spinner style. It is a layout ID provided by the Android system. You can also replace the custom Layout file.
*/
// Arrayadapter <charsequence> adapter = arrayadapter. createfromresource (this, R. array. spinner, Android. R. layout. simple_spinner_item );
// Set the display style of the spinner and reference a style provided by the Android system. The system comes with many styles, large
// You can select your preferred style or custom style. You can also customize the style as needed.
// Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
List <string> List = new arraylist <string> ();
List. Add ("wo ");
List. Add ("AI ");
List. Add ("Ni ");
/**
* Call the arrayadapter constructor to create an arrayadapter object.
* The first parameter indicates the context.
* The second parameter specifies the style of each entry in the drop-down menu.
* The third parameter specifies the ID of the textview space.
* The fourth parameter is the set of data to be added to provide data for the entire list.
*/
Arrayadapter adapter = new arrayadapter (this, R. layout. Item, R. Id. textview, list );
Spinner. setadapter (adapter );
Spinner. setprompt ("test ");
// Set the listener of the spinner Control
Spinner. setonitemselectedlistener (New adapterview. onitemselectedlistener (){
// This method is called when the user selects an entry
/**
* First parameter: indicates that the entire list page contains a list of all entries.
* Second parameter: View of the selected entry
* Third parameter: Location
* Fourth parameter: Space ID
*/
@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
/**
* Getitematposition ()
* Get the specified location in the relevant data list
*/
Toast. maketext (parent. getcontext (), "the planet is" + parent. getitematposition (position). tostring (),
Toast. length_long). Show ();
}
@ Override
Public void onnothingselected (adapterview <?> Parent ){
}
});
}
}
In this way, you can dynamically add data to the list in the list.