Baseadapter for Android basic classes

Source: Internet
Author: User
Baseadapter for Android applications Program It is a commonly used basic data adapter. It is mainly used to transmit a set of data to UI display components such as listview, spinner, gallery, and gridview. It inherits from the interface adapter, 1. Adapter class Introduction 1). shows the structure of the relevant adapter class: to customize the adapter subclass, You need to implement the preceding methods. The most important method is the getview () method, it returns the view component after obtaining data, such as textview in each row of listview and 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 data sources and UI components, the relationship between the adapter, data, and UI can be expressed as follows: 3), common subclass 2, baseadapter Introduction
Baseadapter implements the listadapter and spinneradapter interfaces. Of course, it can also directly provide data to UI components such as listview and spinner. Shows the related class structure. 3. Example 1: Gallery displays the running results of a group of images:
Note: The above row of images is the gallery. Each time you click a gallery image, the layout file of the image is displayed in an uppercase image below:
 <?  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: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: Text = "@ String/hello"
/>

< Gallery
Android: ID = "@ + ID/gallery1"
Android: layout_width = "Match_parent"
Android: spacing = "5px"
Android: layout_height = "Wrap_content"
> </ 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;
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 you click an image, set the resourceid of the image to the image view below,
@ Override
Public Void Onitemclick (adapterview <?> Arg0, view, Int Position,
Long Arg3 ){

Imgview. setimageresource (IMGs [position]);

}
});
}

Class Myimgadapter Extends Baseadapter {
// The custom image adapter contains classes in mainactivity to facilitate access to various variables in mainactivity, especially IMGs arrays.
Private Context context; // Receives the passed context object.
Public Myimgadapter (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 an imageview instance for each data (that is, each image ID,
Imageview IV = New Imageview (context ); // For the context variables passed outside,
Iv. setimageresource (IMGs [position]);
Log. I ( " Magc " , String. valueof (IMGs [position]);
Iv. setlayoutparams ( New Gallery. layoutparams ( 80 , 80 )); // Set the size of each image in the gallery to 80*80.
Iv. setscaletype (imageview. scaletype. fit_xy );
Return IV;
}

}

}
Example 2: Use a prompt box to select the Avatar function (gallery and imageswitcher are used together to display the image) To be continued ......
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: 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.