Android simple case: Inherit Baseadapter implementation Adapter

Source: Internet
Author: User

ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;/*** This class provides data as views. It is the designed to support both ListView and the GridView by * Changing a layout resource the file to inflate. */ Public classMeatadapterextendsBaseadapter {Private FinalLayoutinflater Mlayoutinflater; Private Final intMresourceid; /*** Create A new instance of {@linkMeatadapter}. *     * @paraminflater the layout inflater. * @paramresourceId The resource ID for the layout to be used. The layout should contain an * ImageView with ID of "Meat_image" and a TextView with ID of "meat_tit     Le ". */     PublicMeatadapter (Layoutinflater Inflater,intresourceId) {Mlayoutinflater=Inflater; Mresourceid=resourceId; } @Override Public intGetCount () {returnMeat.MEATS.length; } @Override PublicMeat GetItem (intposition) {        returnMeat.meats[position]; } @Override Public LongGetitemid (intposition) {        returnMeat.meats[position].resourceid; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {         Final  view view;         final  Viewholder holder; if(NULL==Convertview) {View= Mlayoutinflater.inflate (Mresourceid, parent,false); Holder=NewViewholder ();Holder.image=(ImageView) View.findviewbyid (r.id.meat_image); Holder.title=(TextView) View.findviewbyid (r.id.meat_title);        View.settag (holder); } Else{ View = Convertview; Holder = (Viewholder) View.gettag (); } Meat Meat=GetItem (position);        Holder.image.setImageResource (Meat.resourceid);        Holder.title.setText (Meat.title); returnview; }    Private Static class viewholder { PublicImageView image;  PublicTextView title; }}

/*** Sample data.*/ Public classMeat { Public intresourceId;  PublicString title;  PublicMeat (intResourceId, String title) {         This. ResourceId =resourceId;  This. title =title; }     Public Static Finalmeat[] Meats = {            NewMeat (R.DRAWABLE.P1, "first"),            NewMeat (R.DRAWABLE.P2, "Second"),            NewMeat (R.DRAWABLE.P3, "third"),            NewMeat (R.DRAWABLE.P4, "fourth"),            NewMeat (R.DRAWABLE.P5, "fifth"),            NewMeat (R.DRAWABLE.P6, "sixth"),            NewMeat (R.DRAWABLE.P7, "seventh"),            NewMeat (R.DRAWABLE.P8, "eighth"),            NewMeat (R.DRAWABLE.P9, "ninth"),            NewMeat (R.DRAWABLE.P10, "tenth"),            NewMeat (R.DRAWABLE.P11, "eleventh"),    };}

Android simple case: Inherit Baseadapter implementation 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.