Spinner and adapter Modes

Source: Internet
Author: User

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

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.