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; }}