Spinner drop-down list in Android (implemented using Arrayadapter and custom adapter).

Source: Internet
Author: User

Today I learned the spinner component, using spinner as the equivalent of selecting a project from the drop-down list, the following shows the use of spinner (using Arrayadapter and custom adapter implementations respectively)

(a): Use Arrayadapter to fit data:

①: First define a layout file:

<span style= "FONT-SIZE:16PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <spinner        android:id= "@+id/spinner1" android:layout_width= "Match_        Parent "        android:layout_height=" wrap_content "      /></linearlayout></span>

"Note:" The above spinner has two properties 1:prompt is the initial time, spinner displays the data, is a reference type 2:entries is to bind the data source directly in the XML layout file (can not be set, that can be dynamically bound in the activity)

②: Create a data source, use an array, and the data will be displayed in the spinner down list:

<span style= "FONT-SIZE:16PX;" ><?xml version= "1.0" encoding= "Utf-8"?><resources> <string-array    name= "Spinnername" >        <item> Beijing </item>        <item> Shanghai </item>        <item> Guangzhou </item>        <item > shenzhen </item>    </string-array></resources></span>

③: Then add the following code to the activity ( using a system-defined drop-down list of layout files, of course, can also be customized)

               Initialize control Mspinner = (Spinner) Findviewbyid (r.id.spinner1);//Establish data source string[] Mitems = Getresources (). Getstringarray ( R.array.spinnername);//Establish Adapter and bind the data source arrayadapter<string> _adapter=new arrayadapter<string> (This, Android. R.layout.simple_spinner_item, Mitems);//Bind Adapter to control Mspinner.setadapter (_adapter);

The above code initially completed, to see the effect of the operation:

Here is a click event about spinner (for example):

        Mspinner.setonitemselectedlistener (New Onitemselectedlistener () {@Overridepublic void onitemselected (Adapterview <?> parent, View view,int position, long id) {String str=parent.getitematposition (position). ToString (); Toast.maketext (Spinneractivity.this, "You clicked:" +str, "). Show ();} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}});

(ii) Use of custom adapter (emphasis)

①: Define a layout file for each item

<?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:orientation= "horizontal" > <textview android:id= "@+id/text View1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:drawableleft=         "@drawable/ic_launcher" android:paddingright= "8dip" android:paddingtop= "8dip" android:text= "TextView" Android:textsize= "25sp"/> <textview android:id= "@+id/textview2" android:layout_width= "wrap        _content "android:layout_height=" wrap_content "android:paddingleft=" 8dip "android:paddingtop=" 8dip " android:text= "TextView" android:textsize= "25sp"/></LINEARLAYOUT> 

②: Establish the Person class:

Package Com.jiangqq.csdn;public class Person {private string personname;private string personaddress;public person ( String personname, String personaddress) {super (); this.personname = Personname;this.personaddress = personaddress;} Public String Getpersonname () {return personname;} public void Setpersonname (String personname) {this.personname = PersonName;} Public String getpersonaddress () {return personaddress;} public void setpersonaddress (String personaddress) {this.personaddress = personaddress;}}

③: Create Myadapter inheritance and baseadapter to fit:

Package Com.jiangqq.csdn;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.textview;/** * Custom Adapter class * @author jiangqq <a href=http://blog.csdn.net/jiangqq781931404></a > * */public class Myadapter extends Baseadapter {private list<person> mlist;private Context mcontext;public Myad Apter (Context pContext, list<person> pList) {this.mcontext = Pcontext;this.mlist = pList;} @Overridepublic int GetCount () {return mlist.size ();} @Overridepublic Object getItem (int position) {return mlist.get (position);}    @Overridepublic long Getitemid (int position) {return position;} /** * Below is important code */@Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Layoutinflater _la Youtinflater=layoutinflater.from (Mcontext); Convertview=_layoutinflater.inflate (R.layout.item, NULL); if ( Convertview!=null) {TextView _textview1=(TextView) Convertview.findviewbyid (R.ID.TEXTVIEW1); TextView _textview2= (TextView) Convertview.findviewbyid (R.ID.TEXTVIEW2); _textview1.settext (Mlist.get (position). Getpersonname ()); _textview2.settext (Mlist.get (position). Getpersonaddress ());} return Convertview;}}

④: Add the following code to the activity:

                   Initialize control Mspinner = (Spinner) Findviewbyid (r.id.spinner1);//Establish data source    list<person> persons=new ArrayList  <Person> ();p ersons.add (New person ("Zhang San", "Shanghai"));p Ersons.add (New person ("John Doe", "Shanghai");p Ersons.add ("New Person" ("Harry "," Beijing "));p Ersons.add (New Person (" Zhao Liu "," Guangzhou "));//  Establish adapter bound data source Myadapter _myadapter=new Myadapter (This, persons) ;//Binding Adaptermspinner.setadapter (_myadapter);

The results are as follows:

Spinner drop-down list in Android (implemented using Arrayadapter and custom adapter).

Related Article

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.