Android_ custom Simple and fast universal simplebaseadapter

Source: Internet
Author: User

Public abstract class Simplebaseadapter<t> extends Baseadapter {protected Context context;protected list<t> Data;public Simplebaseadapter (Context context) {This (context, null);} Public Simplebaseadapter (context context, list<t> data) {This.context = Context;this.data = data = = null? New Array List<t> (): New arraylist<t> (data);} @Overridepublic int GetCount () {return data.size ();} @Overridepublic Object getItem (int position) {if (position >= data.size ()) return Null;return data.get (position);} @Overridepublic long Getitemid (int position) {return position;} /** * This method requires a subclass implementation and needs to return the resource ID of the item layout * * @return */public abstract int getitemresource ();/** * Replace the original get with the Getitemview method The View method requires a subclass implementation * * @param position * @param convertview * @param parent * @param holder * @return */public abstract View g Etitemview (int position, view Convertview, Viewholder holder), @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder;if (nulL = = Convertview) {Convertview = View.inflate (context, Getitemresource (), null); holder = new Viewholder (Convertview); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} return Getitemview (position, Convertview, holder);} public class Viewholder {private sparsearray<view> views = new Sparsearray<view> ();p rivate View Convertview; Public Viewholder (View convertview) {this.convertview = Convertview;}  @SuppressWarnings ("unchecked") public <t extends view> T getView (int resId) {View v = views.get (resId); if (v = = null) {v = Convertview.findviewbyid (resId); Views.put (ResId, v);} Return (T) v;}} public void AddAll (list<t> elem) {data.addall (elem); notifydatasetchanged ();} public void Remove (T elem) {data.remove (elem); notifydatasetchanged ();} public void Remove (int index) {data.remove (index); notifydatasetchanged ();} public void ReplaceAll (list<t> elem) {data.clear ();d ata.addall (elem); notifydatasetchanged ();}}

Instance Code

Private class Myadapter extends Simplebaseadapter<string> {public Myadapter (context context, list<string> Data) {Super (context, data);} @Overridepublic int Getitemresource () {return r.layout.layout_item;} @Overridepublic view Getitemview (int position, view convertview, Simplebaseadapter<string>. Viewholder holder) {TextView Tvindex = Holder.getview (R.id.item_index); TextView tvcontent = Holder.getview (r.id.item_content); Tvindex.settext ("P:" + position); Tvcontent.settext (GetItem ( Position). toString ()); return Convertview;}}

Android_ custom Simple and fast universal simplebaseadapter

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.