adapter-the typical wording of custom adapter

Source: Internet
Author: User
Tags unique id

Article Reference http://www.cnblogs.com/mengdd/p/3254323.html

ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.TextView;ImportAndroid.widget.ImageView; Private classMyadapterextendsBaseadapter {/** Load volume, general a list of classes, which have their own properties*/        PrivateLayoutinflater Minflater; Privatelist List;  Publicmyadapter (Context context,list List) {//Cache the layoutinflate to avoid asking for a new one each time.Minflater =Layoutinflater.from (context);  This. list=list; }        /*** The number of items in the list are determined by the number of speeches * in our array. * Determines the number of item lines that you display, generally directly returns the length of the container *@seeAndroid.widget.listadapter#getcount ()*/         Public intGetCount () {returnlist.length; }        /*** Since The data comes from an array, just returning the index was * sufficent to get at the data.         If we were using a more complex data * structure, we would return whatever object represents one row in the         * list. * Return Itme object, generally return directly to List[position] *@seeandroid.widget.listadapter#getitem (int)*/         PublicObject GetItem (intposition) {            returnList[position]; }        /*** Use the array index as a unique ID. * Return Item subscript *@seeandroid.widget.listadapter#getitemid (int)*/         Public LongGetitemid (intposition) {            returnposition; }        /*** Make a view to hold each row. * Achieve resource adaptation, this is the most important *@seeandroid.widget.listadapter#getview (int, Android.view.View, * android.view.ViewGroup)*/         PublicView GetView (intposition, View Convertview, ViewGroup parent)             {Viewholder holder; if(Convertview = =NULL) {Convertview= Minflater.inflate (R.layout.list_item_icon_text,NULL); Holder=NewViewholder (); Holder.text=(TextView) Convertview.findviewbyid (R.id.text); Holder.icon=(ImageView) Convertview.findviewbyid (R.id.icon); //Add holderConvertview.settag (holder); } Else{//Get the holderHolder =(Viewholder) Convertview.gettag (); }            //Bind the data efficiently with the holder.Holder.text.setText (list[position]);            Holder.icon.setImageResource ((list[position]); returnConvertview; }/*** A static class created by the holder mode, which actually carries some controls that saves us the hassle of creating them alone. In fact, what we put inside is the control we need to set in our own layout.*/ Static classViewholder {TextView text;        ImageView icon; }    }

adapter-the typical wording of custom adapter

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.