The spinner of widgets

Source: Internet
Author: User

Spinner equivalent to the drop-down list in the Android development documentation for the introduction of spinner:

Android.widget
Class Spinner
Java.lang.Object  android.view.View      android.view.ViewGroup          android.widget.adapterview< spinneradapter>              android.widget.AbsSpinner                  android.widget.Spinner
All implemented interfaces:
dialoginterface.onclicklistener,drawable.callback,keyevent.callback,viewmanager,viewparent
A view that displays one child at a time and lets the user pick among them.
Adapterassociated with this view.
Spinner is a view that can only present one child element at a time and allows the user to select one of its child elements. Its child elements are derived from the adapter that is bound to this view.

From the official documentation we can see that to use spinner must be a adapter, it is this spinner data source.

Let's start with a data source: There are two types of data sources, 1. Create a new array directly 2. Declare in Strings.xml.

Let's first declare an array in Strings.xml:

<string-array name= "Cities" >    <item> Beijing </item>    <item> Shanghai </item></ String-array>
There are only two data, Beijing and Shanghai. Here is the implementation code:

string []cities = string []items = Getresources (). Getstringarray (r.array.cities); Adapter <string>myadapter = new Arrayadapter<string> (this,android. r.layout.simple_spinner_item,cities); Aadapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); Sp_spinner01.setadapter (Aadapter);
This binds the adapter to the spinner. Here is the listener:

Sp_spinner01.setonitemselectedlistener (New Onitemselectedlistener () {@Overridepublic void onitemselected ( Adapterview<?> arg0, View arg1,int arg2, long Arg3) {//TODO auto-generated method STUBLOG.I ("abc", "Arg1=" +arg2+ "Te Xt= "+arg0.getitematposition (arg2). toString ());} @Overridepublic void onnothingselected (adapterview<?> arg0) {//TODO auto-generated method STUBLOG.I ("abc", " You have nothing to choose ");});
Gets the selected content to use: Arg0.getitematposition (ARG2), the second parameter is the subscript of the array where the selected content is located. Such a basic spinner can be run, the result of the operation is as follows:



By convention, we generally do not use the system-provided spinner, generally use custom spinner, from the above example we can see that if you want to use a custom spinner, you need to have a adapter, there is a layout to host each item. The Android system doesn't look good, and I want each item to have a picture and text, so we can define it ourselves:

Create a new items.xml in the layout directory

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" ><imageview     android:id= "@+id/iv_icon"    android:layout_width= "0DP"    android:layout_weight= "5"    android:layout_height= "match_parent"/><textview     android:id= "@+id/tv_ Content "    android:layout_width=" 0DP "    android:layout_weight=" 5 "    android:layout_height=" match_parent "    android:gravity=" center "/></linearlayout>

It contains a imageview and a textview.

The most important thing is our adapter, to inherit from Baseadapter.

Package Com.example.widgetstest;import Java.util.list;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.imageview;import Android.widget.textview;public Class Myownadapter extends Baseadapter {private list<items>mylist;private Context mycontext;public myownadapter (List <items>mylist,context Context) {//TODO auto-generated constructor stubthis.mylist = Mylist;this.mycontext = Context;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn mylist.size ();} @Overridepublic Object getItem (int arg0) {//TODO auto-generated method Stubreturn Mylist.get (arg0);} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn arg0;} @Overridepublic view GetView (int arg0, view arg1, ViewGroup arg2) {//TODO auto-generated method Stublayoutinflater _myinf later = Layoutinflater.from (mycontext); arg1 = _myinflater.inflate (r.layout.Spinner_item, NULL), if (arg1! = null) {TextView content = (TextView) Arg1.findviewbyid (r.id.tv_content); content.settext (Mylist.get (arg0). GetItem ()); int imageicon = R.drawable.ic_launcher;switch (arg0) {Case 0:imageicon = R.drawable.broad    Casting_system_monitor;    Break    Case 1:imageicon = R.drawable.parking_monitor;    Break    Case 2:imageicon = r.drawable.device_alarming; break;} ImageView icon = (ImageView) Arg1.findviewbyid (R.id.iv_icon); Icon.setimageresource (ImageIcon);} return arg1;}}
The GetView method is one of the most important methods here, focusing on the idea of getting the view container of the current context through the Layoutinflater method, and then perhaps our custom item-style XML file, Once acquired, it is possible to do some work on it. Finally, this set-up view is returned.

Create a new items class content as:

Package Com.example.widgetstest;public class Items {items () {}items (String items) {this.item = items;} private string Item;public string GetItem () {return item;} public void SetItem (String item) {This.item = Item;}}
Here is just a simple demonstration, if you want to be more complicated, you can modify it yourself.

Then there is the call in the activity:

Spinner Myspinner = (Spinner) Findviewbyid (R.id.my_spinner); List <items>_items = new arraylist<items> (); _items.add (New items ("alarms")), _items.add (new items ("Parking")), _items.add (New Items ("broadcast")); Myownadapter _myadapter = new Myownadapter (_items,this); Myspinner.setadapter (_myadapter); Myspinner.setonitemselectedlistener (New Onitemselectedlistener () {@Overridepublic void onitemselected (Adapterview <?> arg0, View arg1,int arg2, long arg3) {Items T =  (items) arg0.getitematposition (arg2); LOG.I ("abc", T.getitem (). toString ());} @Overridepublic void onnothingselected (adapterview<?> arg0) {//TODO auto-generated Method stub}});
One thing to note here is that the return value of Getitematposition () is the problem, the first time I return the content using the ToString () method after the display is a sequence of serialized numbers and not what I want to check the API to find:

Public Object getitematposition (int position) {           T adapter = Getadapter ();           Return (adapter = = NULL | | Position < 0)? Null:adapter.getItem (position);      }
This method actually calls the GetItem method in our adapter, and then we look at the definition of the method I wrote:

Public Object getItem (int arg0) {return mylist.get (arg0);}
This returns the data in MyList, and the data that my mylist loads is:
So the return here is necessarily an items object, so you need to:

Items T =  (items) arg0.getitematposition (arg2);
You can then call this item object's Get method to get to this value.

The results of the operation are as follows:









The spinner of widgets

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.