Android Basic Class Baseadapterbaseadapter is a basic data adapter commonly used in Android applications, and its main purpose is to upload a set of data to a ListView, Spinner, UI display components such as gallery and GridView, which are inherited from the interface class adapter,1, adapter class introduction 1), adapter related class structure as shown: Custom adapter subclass, you need to implement the above methods, the most important of which is GetView () method, which returns the view component after fetching the data, such as TextView in each row in the ListView, each ImageView in 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"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<textview
android:text= "@string/hello"
/>
></Gallery>
<imageview
Android:id= "@+id/iv"
Android:layout_gravity= "Center_vertical"
android:layout_margintop= "20px"
Android:layout_width= "320px"
android:layout_height= "320px"
></ImageView>
</LinearLayout>
Mainactivity class:
Package com.magc.adapter;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.View.OnClickListener;
Import Android.widget.AdapterView;
Import Android.widget.BaseAdapter;
Import Android.widget.Gallery;
Import Android.widget.ImageView;
Import Android.widget.AdapterView.OnItemClickListener;
public class Mainactivity extends Activity {
Private Gallery Gallery;
Private ImageView 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 void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Imgview = (ImageView) Findviewbyid (R.ID.IV);
Gallery = (gallery) Findviewbyid (r.id.gallery1);
Myimgadapter adapter = new Myimgadapter (this);
Gallery.setadapter (adapter);
Gallery.setonitemclicklistener (New Onitemclicklistener () {
When the user clicks on the picture, the ResourceID of the picture is set to the ImageView below,
@Override
public void Onitemclick (adapterview<?> arg0, view view, int position,
Long Arg3) {
Imgview.setimageresource (Imgs[position]);
}
});
}
Class Myimgadapter extends Baseadapter {
Custom picture adapter is present in mainactivity as an internal class for easy access to variables in mainactivity, especially IMGs arrays
Private context context;//to receive the context object passed over
Public Myimgadapter (Context context) {
Super ();
This.context = context;
}
/* (Non-javadoc)
* @see Android.widget.adapter#getcount ()
*/
@Override
public int GetCount () {
return imgs.length;
}
/* (Non-javadoc)
* @see Android.widget.adapter#getitem (int)
*/
@Override
Public Object getItem (int position) {
return position;
}
/* (Non-javadoc)
* @see android.widget.adapter#getitemid (int)
*/
@Override
public long getitemid (int position) {
return position;
}
/* (Non-javadoc)
* @see android.widget.adapter#getview (int, android.view.View, android.view.ViewGroup)
*/
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Create a ImageView instance for each data (that is, each picture ID)
ImageView IV = new ImageView (context);//For the context variable passed over outside,
Iv.setimageresource (Imgs[position]);
LOG.I ("MAGC", String.valueof (Imgs[position]));
Iv.setlayoutparams (New Gallery.layoutparams (80, 80));//Set the size of each picture in Gallery to 80*80.
Iv.setscaletype (ImageView.ScaleType.FIT_XY);
return IV;
}
}
}
Android Baseadapter Adaptation base class