Android:listview (i)

Source: Internet
Author: User

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)

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.