Android Basic Class Baseadapter

Source: Internet
Author: User

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://"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.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 (; 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

Related Article

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: 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.