The spinner is equivalent to the select drop-down list in an HTML form.
------------
| Method 1 |
------------
Add an array in string. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "spinner_data">
<Item> Beijing </item>
<Item> Hunan </item>
<Item> Hubei </item>
</String-array>
</Resources>
Drag a spinner:
<Spinner
Android: Id = "@ + ID/spinner1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: entries = "@ array/spinner_data"
Android: spinnermode = "dialog"
/>
<! -- Entries: displays the data items in the spinner.
Spinnermode = "dropdown" is the default drop-down list, And spinnermode = "dialog" is displayed in the form of a dialog box --!>
------------
| Method 2 |
------------
Baseadapter is a commonly used basic data adapter in Android applications. It is mainly used to transmit a set of data to UI display components such as listview, spinner, gallery, and gridview, it automatically inherits the adapter of the interface class.
If you want to add string [] or list to the spinner.
Bytes --------------------------------------------------------------------------------------------------------------
A) string []
Define a string array in the mainactivity main class:
Private string [] STR = new string [] {
"Shandong", "Shanxi", "Beijing"
};
Define a class that inherits the baseadapter:
Private class myadapter extends baseadapter {
@ Override
Public int getcount (){
// Todo auto-generated method stub
Return Str. length; // Important Method
}
@ Override
Public object getitem (INT arg0 ){
// Todo auto-generated method stub
Return NULL;
}
@ Override
Public long getitemid (INT arg0 ){
// Todo auto-generated method stub
Return 0;
}
@ Override
Public View getview (INT position, view, viewgroup group) {// Important Method
// Todo auto-generated method stub
// Str. Length
Textview = new textview (mainactivity. This );
Textview. settext (STR [position]);
Return textview;
}
}
In the oncreate method:
Spinner spinner = (spinner) findviewbyid (R. Id. spinner1 );
Spinner. setadapter (New myadapter ());
Bytes --------------------------------------------------------------------------------------------------------------
B) List
Define a list in the mainactivity main class:
Private list = new arraylist ();
Add the following content to the oncreate method:
List. Add ("Shanghai ");
List. Add ("Tianjin ");
List. Add ("Zhejiang ");
The myadapter class only needs to be modified in a few places:
1. Public int getcount (){
Return list. Size ();
}
2. Public View getview (INT position, view, viewgroup group ){
Textview = new textview (mainactivity. This );
Textview. settext (charsequence) list. Get (position ));
Return textview;
}
3. You can also add the onitemselectedlistener event to the spinner:
Spinner. setonitemselectedlistener (New onitemselectedlistener (){
@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
// Todo auto-generated method stub
Toast. maketext (mainactivity. This, (charsequence) list. Get (position), 0). Show ();
}
@ Override
Public void onnothingselected (adapterview <?> Parent ){
// Todo auto-generated method stub
}
});
Bytes --------------------------------------------------------------------------------------------------------------
Spinner and adapter Modes