The primary purpose of baseadapter is to upload a set of data to a UI display component such as a ListView, Spinner, Gallery, and GridView, which is inherited from the interface class, for the basic data adapter commonly used in Android applications adapter,1 , adapter class Introduction 1), adapter related class structure as shown: Custom adapter subclasses, you need to implement the above methods, the most important of which is the GetView () method, which is to get the data after the view component returned, such as the TextView in each row in the ListView, each ImageView in the gallery. 2), adapter plays a very important role in Android applications, and is widely used, it can be seen as a bridge between the data source and UI components, where adapter, the relationship between data and UI, can be represented by: 3), common subclass 2, Baseadapter Introduction
Baseadapter is the implementation of the ListAdapter and Spinneradapter two interfaces, of course, it can also directly provide data to the UI components such as ListView and spinner. The related class structure is as shown in: 3, Example one: Gallery displays a set of picture run results:
Description: The above line of pictures is Gallery Gallery, each time you click on a gallery picture, the picture layout file will be displayed as a large image in the following form:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" ><TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello" /> <GalleryAndroid:id= "@+id/gallery1"Android:layout_width= "Match_parent"android:spacing= "5px"Android:layout_height= "Wrap_content" ></Gallery><ImageViewAndroid:id= "@+id/iv"android:layout_gravity= "Center_vertical"Android:layout_margintop= "20px"Android:layout_width= "320px"Android:layout_height= "320px" ></ImageView> </LinearLayout>
Mainactivity class:
PackageCom.magc.adapter;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.AdapterView;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Gallery;ImportAndroid.widget.ImageView;ImportAndroid.widget.AdapterView.OnItemClickListener; Public classmainactivityextendsActivity {PrivateGallery Gallery; PrivateImageView Imgview; Private int[] IMGs={R.DRAWABLE.A6,R.DRAWABLE.A1,R.DRAWABLE.A2,R.DRAWABLE.A3,R.DRAWABLE.A4,R.DRAWABLE.A5}; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Imgview=(ImageView) Findviewbyid (R.ID.IV); Gallery=(Gallery) Findviewbyid (r.id.gallery1); Myimgadapter Adapter= NewMyimgadapter ( This); Gallery.setadapter (adapter); Gallery.setonitemclicklistener (NewOnitemclicklistener () {//when the user clicks on the picture, the ResourceID of the picture is set to the ImageView below,@Override Public voidOnitemclick (Adapterview<?>arg0, view view,intposition,LongArg3) {Imgview.setimageresource (imgs[position]); } }); } classMyimgadapterextendsBaseadapter { //Custom picture adapter is present in mainactivity as an internal class for easy access to various variables in mainactivity, especially IMGs arrays Privatecontext context;//used to receive the context object passed over PublicMyimgadapter (Context context) {Super(); This. Context=context; } /*(non-javadoc) * @see android.widget.adapter#getcount ()*/@Override Public intGetCount () {returnimgs.length; } /*(non-javadoc) * @see android.widget.adapter#getitem (int)*/@Override PublicObject GetItem (intposition) { returnposition; } /*(non-javadoc) * @see android.widget.adapter#getitemid (int)*/@Override Public LongGetitemid (intposition) { returnposition; } /*(non-javadoc) * @see android.widget.adapter#getview (int, android.view.View, android.view.ViewGroup) */@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { //Create a ImageView instance for each data (that is, each picture ID)ImageView IV= NewImageView (context);//for the context variables passed over outside,Iv.setimageresource (imgs[position]); LOG.I ("MAGC", String.valueof (Imgs[position])); Iv.setlayoutparams (NewGallery.layoutparams ( the, the));//set the size of each picture in gallery to 80*80. Iv.setscaletype (ImageView.ScaleType.FIT_XY); returnIV; } } }
Android Basic Class Baseadapter