ListView Four types of adapters:
1, Arrayadapte
2, Simpleadapter
3, Simplecursoradapter
4, Baseadapter
The following is based on the implementation of the adapter in the next 3, there is a simpleadapter of the enhanced version, that is plus, haha. The main is to achieve with picture function, the first adapter is relatively simple, ready to use it to implement display grouping. Here are the implementation features and code.
The core code of the implementation:
Public class ListView1 extends Activity { PrivateListView ListView;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.LAYOUT.LISTVIEW1); ListView = (ListView) This. Findviewbyid (R.id.listview1); Showsimpleadapter (); }Private void Showsimpleadapter() {listNewArraylist for(inti =0; I <4; i++) {hashmap<string, object> item =NewHashmap<string, object> (); Item.put ("Name","ATM"+i); Item.put ("Age",Ten+i); Data.add (item); }/** * Context contexts contextual information list<? extends Map<string,?>> data shows this to the interface int resource Where the data needs to be displayed string[] from where int[] to show where * *Simpleadapter adapter =NewSimpleadapter ( This, data, R.layout.item,Newstring[]{"Name","Age"},New int[]{r.id.text1,r.id.text2}]; Listview.setadapter (adapter); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }}
The core code of the implementation:
Public class ListView2 extends Activity { PrivateListView ListView;PrivateServicetest servicetest;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.LAYOUT.LISTVIEW1); ListView = (ListView) This. Findviewbyid (R.id.listview1); servicetest=NewServicetest (Getapplicationcontext ()); Showsimplecursoradapter (); }Private void Showsimplecursoradapter() {cursor cursor =servicetest.find_cursor (); Simplecursoradapter adapter =NewSimplecursoradapter ( This, R.layout.item, Cursor,Newstring[]{"Name","Age"},New int[]{r.id.text1, r.id.text2}]; Listview.setadapter (adapter); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {Getmenuinflater (). Inflate (R.menu.main, menu);return true; }}
The core code of the implementation:
Public class ListView3 extends Activity { PrivateListView ListView;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.LAYOUT.LISTVIEW2); ListView = (ListView) This. Findviewbyid (R.ID.LISTVIEW2); Showsimpleadapter (); }Private void Showsimpleadapter() {Simpleadapter adapter =NewSimpleadapter ( This, GetData (), R.layout.item_plus,Newstring[]{"Name","Age","img"},New int[]{r.id.name,r.id.age,r.id.img}]; Listview.setadapter (adapter); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }PrivateList<map<string, object>>GetData() {list<map<string, object>> List =NewArraylist<map<string, object>> (); map<string, object> map =NewHashmap<string, object> (); Map.put ("Name","ATM1"); Map.put ("Age",1); Map.put ("img", R.DRAWABLE.IMG2); List.add (map); Map =NewHashmap<string, object> (); Map.put ("Name","ATM2"); Map.put ("Age",2); Map.put ("img", R.drawable.love); List.add (map); Map =NewHashmap<string, object> (); Map.put ("Name","ATM3"); Map.put ("Age",3); Map.put ("img", R.DRAWABLE.IMG1); List.add (map);returnList } }
The core code of the implementation:
Public class ListView4 extends Activity { PrivateListView ListView;PrivateServicetest servicetest;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.LAYOUT.LISTVIEW1); ListView = (ListView) This. Findviewbyid (R.id.listview1); servicetest=NewServicetest ( This); Listview.setonitemclicklistener (NewItemclicklistener ()); Showmyadapter (); }//Click to get the data on the view Private Final class Itemclicklistener implements Onitemclicklistener{ Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {ListView listview = (listview) Parent; UserInfo UserInfo = (UserInfo) listview.getitematposition (position); Toast.maketext (Getapplicationcontext (), userinfo.getuser_id () +" "+userinfo.getuser_name (),1). Show (); } }Private void Showmyadapter() {//TODO auto-generated method stubList<userinfo> List=servicetest.findall (); Myadapter myadapter=NewMyadapter ( This, list, r.layout.item); Listview.setadapter (Myadapter); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; } }
Code download Please click: code download
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android:listview (i)