How to use Android ListView Baseadapter

Source: Internet
Author: User

Usually we have a fixed pattern when we use our own custom adapter. Take advantage of the way convertview+ caches.


Private arraylist<listbean> list;p rivate layoutinflater minflater;public detaillistadapter (context context, Arraylist<listbean> list) {This.list=list;minflater = Layoutinflater.from (context);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn list = = null? 0:list.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic long Getitemid (int position) {return position;}  @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder = null;if (Convertview = = NULL) {Convertview = minflater.inflate (r.layout.search_info_item_layout, null); holder = new Viewholder (); holder. Timetxt = (TextView) Convertview.findviewbyid (R.ID.TIME_TV); holder. Addresstxt = (TextView) Convertview.findviewbyid (R.ID._ADDRESS_TV); holder.processflagtxt = (TextView) Convertview.findviewbyid (R.ID.PROCESS_FLAG_TV); Convertview.settag (holder);} Else{holder = (Viewholder) Convertview.gettag ();} Holder. TImetxt.settext (List.get (position). GetTime ()); holder. Addresstxt.settext (List.get (position). GetAddress ()); Holder.processFlagTxt.setText (List.get (position). GetState ( ); return Convertview;} Class Viewholder{textview Timetxt; TextView Addresstxt; TextView processflagtxt;}}
Recently, do Baidu map offline download time, found a new way, very want to know how the Android program of Baidu Ape is how to write code. In a careful look, there is a very big difference indeed.

From the Baidu map offline download demo

Private Arraylist<mkolupdateelement> localmaplist = Null;public Class Localmapadapter extends Baseadapter {@ overridepublic int GetCount () {return localmaplist.size ();} @Overridepublic Object getItem (int index) {return localmaplist.get (index);} @Overridepublic long getitemid (int index) {return index;} @Overridepublic view GetView (int index, view view, ViewGroup arg2) {mkolupdateelement e = (mkolupdateelement) GetItem (Inde x); view = View.inflate (offlinedemo.this,r.layout.offline_localmap_list, null); Initviewitem (view, E); return view;} void Initviewitem (view view, Final Mkolupdateelement e) {button display = (Button) View.findviewbyid (R.id.display); Button remove = (Button) View.findviewbyid (R.id.remove); TextView title = (TextView) View.findviewbyid (r.id.title); TextView update = (TextView) View.findviewbyid (r.id.update); TextView ratio = (TextView) View.findviewbyid (r.id.ratio) ratio.settext (e.ratio + "%"); Title.settext (e.cityname); if ( E.update) {Update.settext ("updatable");} else {Update.settexT ("Newest");} if (e.ratio! =) {display.setenabled (false);} else {display.setenabled (true);} Remove.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {moffline.remove (E.cityid); Updateview ();}}); Display.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.putextra ("x", E.geopt.longitude); Intent.putextra ("Y", e.geopt.latitude); Intent.setclass (OfflineDemo.this, Basemapdemo.class); startactivity (intent);}});}



How to use Android ListView Baseadapter

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.