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.