Learn about Android custom spinner adapters _android

Source: Internet
Author: User

This article for you to share the Android custom spinner adapter's related knowledge point, for everybody's reference, the specific content is as follows

First, the general effect

Two. Key code

Let's focus on the comments.
(1) Spinner layout: car_brand_spinner.xml
That is to bounce out of the layout of the Drop-down list, the back of that layout will not take out a disgrace, anyway know that there is a spinner ID for the Carbrandspinner can be.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <relativelayout android:layout_width=" match_parent "android:layout_height=" Match_parent "android:l ayout_margin= "10DP" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_cont" Ent "android:text=" "New text" android:id= "@+id/car_brand_name" android:layout_gravity= "left" Androi D:layout_alignparentleft= "true" android:layout_margintop= "5DP" android:layout_marginbottom= "5DP"/> &L T;imageview android:layout_width= "wrap_content" android:layout_height= wrap_content "android:id=" @+id/ca R_brand_flag "android:layout_gravity=" right "android:layout_alignparentright=" true "/> </relativelayou
 T> </LinearLayout>

(2) Adapter

Arrayadapter carbrandadapter=new arrayadapter<string> (Addcaractivity.this, Android. R.layout.simple_spinner_dropdown_item, carbrandnamelist//is string[], is all brandname to display {@Override public View Getdro Pdownview (int position, View Convertview, ViewGroup parent) {Convertview = View.inflate (addcaractivity.this,r.layout.c Ar_brand_spinner,null)//Get Spinner Layout view if (convertview!=null) {TextView Carbrandnameview = (TextView) convertview
    . Findviewbyid (R.id.car_brand_name);
    ImageView Carbrandflagview = (imageview) Convertview.findviewbyid (R.id.car_brand_flag);
      try {jsonobject json = new Jsonobject (carbrandlist.get (position). Get ("Carbrand"). toString ()); Carbrandnameview.settext (json.getstring ("Carbrandname"))//Set data, my data here is read from the server, so there is a process of converting values before}catch (Exception e) {} Bitmap Bitmap =common.string2bitmap (carbrandlist.get (position). Get ("Carbrandflagcontent"). toString ());// Here too, picture data comes from the server, and there is a process of bitmap the data from a string to the IF (Bitmap!=nulL) carbrandflagview.setimagebitmap (bitmap);//Show Picture} return Convertview;
}
};
Give the Spinner set adapter Spinner carbrandspinner= (Spinner) Findviewbyid (R.id.carbrandspinner);
Carbrandspinner.setadapter (Carbrandadapter);
   Carbrandspinner.setonitemselectedlistener (New Adapterview.onitemselectedlistener () {@Override//rewrite event selected by item public void onitemselected (adapterview<?> parent, view view, int position, long id) {} @Override public void O
 Nnothingselected (adapterview<?> parent) {}});

It's over!

This is all about the Android spinner adapter, I hope to help you learn.

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.