Adapter is an important part of Android. Many controls need to be used as data sources. This program uses simpleadapter as the listview data source.
The layout file contains a listview and the imageview and textview displayed in the listview.
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Define a list --> <listview Android: Id = "@ + ID/mylist" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <! -- Define an imageview as part of the list item. --> <Imageview Android: Id = "@ + ID/Header" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: paddingleft = "10dp"/> <! -- Defines a textview that is used as part of the list item. --> <Textview Android: Id = "@ + ID/Name" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textsize = "16dp" Android: gravity = "center_vertical" Android: paddingleft = "10dp"/> </linearlayout>
Use List, imageview, and textview to construct simpleadapter.
Public class simpleadaptertest extends activity {private string [] names = new string [] {"kite", "Perfect Day", "time", "Stefanie "}; private int [] imageids = new int [] {R. drawable. sunyz_1, R. drawable. sunyz_6, R. drawable. sunyz_7, R. drawable. sunyz_8}; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // create a list set. The elements of the list set are maplist <Map <string, Object> listitems = new arraylist <Map <string, Object> (); for (INT I = 0; I <names. length; I ++) {Map <string, Object> listitem = new hashmap <string, Object> (); listitem. put ("CDS", imageids [I]); listitem. put ("cdname", Names [I]); listitems. add (listitem) ;}// create a simpleadaptersimpleadapter simpleadapter = new simpleadapter (this, listitems, R. layout. main, new string [] {"cdname", "CDS"}, new int [] {R. id. name, R. id. header}); listview list = (listview) findviewbyid (R. id. mylist); // set adapterlist for listview. setadapter (simpleadapter );}}