One, Arrayadapter ListView listview= (ListView) Findviewbyid (R.id.list_view);//the parameter of the ListView is the IDListview.setadapter (NewArrayadapter<person> ( ThisAndroid. R.layout.simple_list_item_1, list));//The object is person, the first argument is the context, the second is the template to be displayed, the last is the data to be displayed, and the list is the ArrayList collection of the person class. Second, Baseadapter1, a row of display objects ①, define Myadapter to inherit BaseadapterclassMyadapter extends Baseadapter {@Override Public intGetCount () {returnList.size ();//List for the person object} @Override PublicObject GetItem (intposition) {return NULL; } @Override Public LongGetitemid (intposition) {return 0; }/** * The cache is the masked line*/@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {TextView TextView=NULL; if(NULL!=Convertview) {TextView=(TextView) Convertview; } Else{TextView=NewTextView (mainactivity. This); } textview.settext (list.Get(position). ToString ()); returnTextView; }}②, set adapter ListView ListView=(ListView) Findviewbyid (R.id.list_view); Listview.setadapter (NewMyadapter ()); 2, customizing an XML, adding a row of ① to the ListView, defining your own content layout file to display a row----List_item.xml②, define Myadapter to inherit BaseadapterclassMyadapter extends Baseadapter {@Override Public intGetCount () {returnlist.size (); } @Override PublicObject GetItem (intposition) {return NULL; } @Override Public LongGetitemid (intposition) {return 0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {//layout converters are about converting a layout to an objectLayoutinflater Flater = MainActivity1. This. Getlayoutinflater (); View View= Flater.inflate (R.layout.list_item,NULL);//actually convert a layout file to an object//find the component that corresponds to an ID on a specific objectTextView Text_name =(TextView) View.findviewbyid (r.id.list_view_name); TextView Text_age=(TextView) View.findviewbyid (r.id.list_view_age); Person Person= list.Get(position); Text_name.settext (Person.getname ()); Text_age.settext (String.valueof (Person.getage ())); returnview; }}③, set adapter ListView ListView=(ListView) Findviewbyid (R.id.list_view); Listview.setadapter (NewMyadapter ()); Three, Simpleadapter, the display of a line containing multiple rows of data ①, define their own row to display the layout file to be displayed in the multi-line----List_item.xml②, set the adapter (the code means that the multiline XML to be displayed is a row of name, one line of age); ListView ListView=(ListView) Findviewbyid (R.id.list_view); List<map<string, string>> data =NewArraylist<map<string, string>>(); Map<string, string> info =NewHashmap<string, string>(); Info.put ("name","ZS"); Info.put (" Age"," -"); Data.add (info); Info=NewHashmap<string, string>(); Info.put ("name","Wangwu"); Info.put (" Age","111"); Data.add (info); Info=NewHashmap<string, string>(); Info.put ("name","Wangwu"); Info.put (" Age","111"); Data.add (info); Info=NewHashmap<string, string>(); Info.put ("name","Wangwu"); Info.put (" Age","111"); Data.add (info); Info=NewHashmap<string, string>(); Info.put ("name","Wangwu"); Info.put (" Age","111"); Data.add (info); Info=NewHashmap<string, string>(); Info.put ("name","Wangwu"); Info.put (" Age","111"); Data.add (info); Simpleadapter Simple=NewSimpleadapter ( This, data, R.layout.list_item,NewString[] {"name"," Age"},New int[] {r.id.list_view_name, r.id.list_view_age}); Listview.setadapter (simple);
Two types of adapters in the ListView