Android Custom Spinner Drop-down list (implemented using Arrayadapter and custom adapter) _android

Source: Internet
Author: User

Today, I learned the use of spinner components, a very useful component, the equivalent from the Drop-down list to select items, today a lot of harvest, the following to show you the use of spinner (respectively using Arrayadapter and custom adapter implementation), the specific contents are as follows.

(i): Use of Arrayadapter for data adaptation:

①: 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, spinner display of data, is a reference type 2:entries is directly in the XML layout file binding data source (you can not set, that is, can be dynamically bound in the activity)

②: Set up a data source, using an array, which will be displayed in the spinner 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 (the layout file using the system-defined Drop-down list, of course, can be customized)

    Initializes the control
Mspinner = (Spinner) Findviewbyid (r.id.spinner1);
Set up 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 completes initially, looks down the operation effect:

The following is a click event on spinner (the effect is shown above):

Mspinner.setonitemselectedlistener (New Onitemselectedlistener () {
  @Override public
  void onitemselected ( Adapterview<?> Parent, view view,
      int position, long id) {
    String str=parent.getitematposition (position) . toString ();
    Toast.maketext (Spinneractivity.this, "What you clicked is:" +str). Show ();
  @Override public
  void onnothingselected (adapterview<?> parent) {
    //TODO auto-generated method stub
  }
});

(ii) Use of customized 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:o" rientation= "Horizontal" >
  <textview
    android:id= "@+id/textview1" 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>

②: Create 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 Myada
  Pter extends Baseadapter {private list<person> mlist;
  Private context Mcontext;
    Public Myadapter (Context PContext, list<person> plist) {this.mcontext = PContext;
  This.mlist = plist;
  @Override public int GetCount () {return mlist.size ();
  @Override public Object getitem (int position) {return mlist.get (position);
  @Override public long getitemid (int position) {return position; /** * Below is an important code * * @Override public View getview (int position, View Convertview, ViewGroup parent) {Layou
    Tinflater _layoutinflater=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:

 Initializes the control
Mspinner = (Spinner) Findviewbyid (r.id.spinner1);
Establish the data source
list<person> persons=new arraylist<person> ();
Persons.add (New person ("John", "Shanghai"));
Persons.add (New person ("Dick", "Shanghai"));
Persons.add (New person ("Harry", "Beijing"));
Persons.add (New Person ("Zhao Liu", "Guangzhou"));
Establish adapter bound data source
myadapter _myadapter=new Myadapter (this, persons);
Binding Adapter
Mspinner.setadapter (_myadapter);

The results of the operation are screenshot:


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.