Android Adapter Public notation

Source: Internet
Author: User

In the development process, will write a lot of adapter class, many of the public parts, do not need to write every time, can save a lot of time for developers

Extract a Listviewadapter

 Public Abstract classListviewadapter<t>extendsBaseadapter {//to make the subclass accessible, set the property to protected    protectedContext Mcontext; protectedList<t>Mdatas; protectedLayoutinflater Minflater; Private intLayoutID;//The item layout of different ListView can be different, so we need to extract the layout separately.     PublicListviewadapter (context context, list<t> datas,intLayoutID) {         This. Mcontext =context; Minflater=Layoutinflater.from (context);  This. Mdatas =datas;  This. LayoutID =LayoutID; } @Override Public intGetCount () {returnmdatas.size (); } @Override PublicT GetItem (intposition) {        returnMdatas.get (position); } @Override Public LongGetitemid (intposition) {        returnposition; }    /*** Add Data, * *@paramalist*/     Public voidAddData (list<t>alist) {        if(Alist! =NULL&& alist.size () > 0) {mdatas.addall (alist);    } notifydatasetchanged (); }    /*** Get Data * *@return     */     PublicList<t>GetData () {returnMdatas; }    /*** SET Data, * *@paramsList*/     Public voidSetData (list<t>sList)        {mdatas.clear ();    AddData (sList); }    /*** Erase Data*/     Public voidClear () {mdatas.clear (); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {        //Initialize the Viewholder, use the generic viewholder, just like the code to get the Viewholder initialized.Viewholder holder = Viewholder.get (Mcontext, Convertview, parent, layoutid, position);//LayoutID is the layout of a single itemCONVERT (holder, GetItem (position)); returnHolder.getconvertview ();//This line of code should be taken care of.    }    //Publish the Convert method     Public Abstract voidCONVERT (viewholder holder, T t);}
View Code

Reusable Viewholder Code

 Public classViewholder {PrivateSparsearray<view>mviews; Private intmposition; PrivateView Mconvertview;  PublicViewholder (Context context, viewgroup parent,intLayoutID,intposition) {         This. mposition =position;  This. Mviews =NewSparsearray<view>(); Mconvertview= Layoutinflater.from (context). Inflate (LayoutID, parent,false); Mconvertview.settag ( This); }     Public StaticViewholder Get (context context, View Convertview, ViewGroup parent,intLayoutID,intposition) {        if(Convertview = =NULL) {            return NewViewholder (context, parent, layoutid, position); } Else{Viewholder Holder=(Viewholder) Convertview.gettag (); Holder.mposition= position;//Instant Viewholder is reusable, but position remember to update            returnHolder; }    }    /*get control by Viewid*/    //using generic T, return a subclass of view     Public<textendsView> T GetView (intviewId) {View View=Mviews.get (viewId); if(View = =NULL) {View=Mconvertview.findviewbyid (viewId);        Mviews.put (viewId, view); }        return(T) view; }     PublicView Getconvertview () {returnMconvertview; }}
View Code

And look at the adapter class now.

 Public class extends Listviewadapter<string> {    public msgtemplateadapter (context context, list<string > datas) {        Super(context, datas, R.layout.template_list_item);    }    @Override    publicvoid  convert (viewholder holder, String s) {        (TextView) Holder.getview (R.id.name)). SetText (s);    }}
View Code

See this concise code, is not feel the whole people are spirit

Reference: http://www.cnblogs.com/smyhvae/p/4477079.html

"declaration"

Welcome reprint, but please keep the original source of the article

Blog Address: http://www.cnblogs.com/lping/

Article Source: http://www.cnblogs.com/lping/articles/5470781.html

Android Adapter Public notation

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.