Android: Custom Adapter

Source: Internet
Author: User

Both Arrayadapter and simpleadapter inherit the Baseadapter, and the custom adapter also inherits Baseadapter

Example: Gallery Implementing a picture browser

<? XML version= "1.0" encoding= "Utf-8" ?> < Gallery   xmlns:android = "Http://schemas.android.com/apk/res/android"     android:id= "@+id/gallery"    android:layout_width= "Match_parent"      android:layout_height= "wrap_content">    </ Gallery >

 Public classMainactivityextendsActivity {PrivateGallery Gallery;Private int[] res={r.drawable.ic_launcher,r.drawable.ic_launcher};PrivateImageadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.demo); Gallery=(Gallery) Findviewbyid (r.id.gallery); Adapter=NewImageadapter (Res, This);                            Gallery.setadapter (adapter); }}

Custom Adapters

 Public classImageadapterextendsbaseadapter{ Public intres[]; Privatecontext Context;  PublicImageadapter (intRes[],context Context) {         This. res=Res;  This. context=context; } @Override//returns the total number of defined data sources     Public intGetCount () {//TODO auto-generated Method Stub        returnres.length; } @Override//tells the adapter to get the data object in the current container     PublicObject GetItem (intposition) {        //TODO auto-generated Method Stub        returnposition; } @Override//tells the adapter to get the data ID in the current container     Public LongGetitemid (intposition) {        //TODO auto-generated Method Stub        returnposition; } @Override//Get the image view you want to display now     PublicView GetView (intposition, View Convertview, ViewGroup parent) {        //TODO auto-generated Method StubImageView image=NewImageView (context);        Image.setbackgroundresource (Res[position]); Image.setlayoutparams (NewGallery.layoutparams (400,300));        Image.setscaletype (SCALETYPE.FIT_XY); returnimage; }}

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.