Android spinner controls in a detailed

Source: Internet
Author: User

Spinner class Diagram

Android.widget class Spinnerjava.lang.Object  android.view.View      android.view.ViewGroup          Android.widget.AdapterView<SpinnerAdapter>              android.widget.AbsSpinner                  Android.widget.Spinner

Spinner means a drop-down list component.

The following is the contents of the Android website document.

Add the Spinner element tag to the layout file

< Spinner     Android:id = "@+id/planets_spinner"     android:layout_width= "Fill_parent"    android:layout_height= "Wrap_ Content "/>

To populate the spinner with a list of choices, and then need to specify a in SpinnerAdapter your Activity or Fragment source code.

To add a selection to spinner, you need to bind to the spinner object as a data source using a specific Spinneradapter object in your activity or fragment.

The choices provide for the spinner can come from any source, but must is provided through an SpinnerAdapter , such as an ArrayAdapter I f The choices is available in an array or a CursorAdapter if the choices is available from a database query.

Although you can provide spinner with any data source, this data source must be implemented with the Spinneradapter interface. such as Arrayadapter. If you choose to query the data from the database, you can use the adapter as the data source CursorAdapter.

You can use a string array.

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <String-arrayname= "Planets_array">        <Item>Mercury</Item>        <Item>Venus</Item>        <Item>Earth</Item>        <Item>Mars</Item>        <Item>Jupiter</Item>        <Item>Saturn</Item>        <Item>Uranus</Item>        <Item>Neptune</Item>    </String-array></Resources>

With a array such as this one, you can use the following code in your Activity or to supply the spinner with the Fragment array Using an instance of ArrayAdapter :

If you use a string array, you need to use arrayadapter in your activity or fragment.

Spinner Spinner = (Spinner) Findviewbyid (r.id.spinner); // Create an Arrayadapter using the string array and a default spinner layout arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (This,        r.array.planets _array, Android. R.layout.simple_spinner_item); // Specify the layout to use when the list of choices appears Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); // Apply The adapterto the spinner Spinner.setadapter (adapter);

static method Createfromresource ( Context context, int textArrayResId, int textViewResId )

Parameter context Use this here means activity

Parameter Textarrayresid use R.array.plants_array data source here

Parameters textViewResId use Android here. R.layout.simple_spinner_item (Android provided also customizable) spinner layout style

You should then call to setDropDownViewResource(int) specify the layout the adapter should use to display the list of spinner choices (is simple_spinner_dropdown_item a nother standard Layout defined by the platform).

You can call Setdropdownviewresource (int) to specify the layout and style of the options in the spinner list.

How to respond to user's choice, implement Onitemselectedlistener interface

 Public classSpinneractivityextendsActivityImplementsOnitemselectedlistener {... Public voidOnitemselected (adapterview<?>Parent, view view,intPosLongID) {//An item is selected. You can retrieve the selected item using//parent.getitematposition (POS)    }     Public voidOnnothingselected (adapterview<?>parent) {        //another interface callback    }}

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods.

Where this interface needs to implement two of these methods

Then you need to specify the interface implementation by calling setOnItemSelectedListener() : (Specify Spinner Object Implementation Onitemselectedlistener interface)

Spinner Spinner = (Spinner) Findviewbyid (R.id.spinner); Spinner.setonitemselectedlistener (this);

Android spinner controls in a detailed

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.