Use of Android Baseadapter

Source: Internet
Author: User

There are many kinds of data adapters, which are recorded here today the most common is the adapter baseadapter.

First of all, what is an adapter, here I find a picture from the Internet

It is easy to see that the so-called adapter is the bridge between the data and the view. It binds the data to the ListView control.

Specifically how to bind it, today to record it. First, let's look at it first.

This is a simple picture, title, and content layout that I wrote. About the ListView I would also like to point out that it is the caching mechanism. It puts all the content that is not displayed on the current page into the cache, and when the page slides up, the part that goes beyond the screen is placed in the cache, and the newly displayed part is taken directly from the cache.

1. First create a new Android.xml file to display the style for each row of the ListView.

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <ImageView Android:id= "@+id/item_img"Android:layout_width= "60DP"Android:layout_height= "60DP"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"android:src= "@drawable/ic_launcher"/> <TextView Android:id= "@+id/item_title"Android:layout_width= "Match_parent"Android:layout_height= "30DP"Android:layout_alignparenttop= "true"Android:layout_torightof= "@+id/item_img"Android:text= "title"android:textsize= "25SP"android:gravity= "Center_horizontal"/> <TextView Android:id= "@+id/item_content"Android:layout_width= "Match_parent"Android:layout_height= "30DP"Android:layout_torightof= "@+id/item_img"Android:layout_below= "@+id/item_title"Android:layout_alignparentright= "true"Android:text= "Content"android:textsize= "15SP"android:gravity= "Center_vertical"/></relativelayout>
View Code

2. Define a Bean object to hold the data to be displayed for each row of the ListView

 PackageCom.example.baseadapter; Public classItembean { Public intItemimgresid;  PublicString Itemtitle;  PublicString itemcontent;  PublicItembean (intitemimgresid,string itemtitle,string itemcontent) {                 This. Itemimgresid =Itemimgresid;  This. Itemtitle =Itemtitle;  This. itemcontent =itemcontent; }}
View Code

3, customize the adapter and inherit Baseadapter and rewrite the 4 methods inside.

One of the fourth most important is to get what each item will show.

 PackageCom.example.baseadapter;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public classMyadapterextendsBaseadapter {PrivateList<itembean>listdate; PrivateLayoutinflater Mlayoutinflater;  PublicMyadapter (Context context,list<itembean>Listbean) {Mlayoutinflater=Layoutinflater.from (context);  This. listdate =Listbean; } @Override//to get the number of data in a data set     Public intGetCount () {//TODO auto-generated Method Stub        returnlistdate.size (); } @Override//gets the data item for the indexed data set in the dataset     PublicObject GetItem (intposition) {        //TODO auto-generated Method Stub        returnListdate.get (position); } @Override//gets the corresponding ID for the specified row     Public LongGetitemid (intposition) {        //TODO auto-generated Method Stub        returnposition; } @Override//get the display of each item     PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder Viewholder=NULL; if(Convertview = =NULL) {Viewholder=NewViewholder (); Convertview= Mlayoutinflater.inflate (R.layout.itemlist,NULL); //instantiating a controlViewholder.img =(ImageView) Convertview.findviewbyid (r.id.item_img); Viewholder.texttitle=(TextView) Convertview.findviewbyid (r.id.item_title); Viewholder.textcontent=(TextView) Convertview.findviewbyid (r.id.item_content);        Convertview.settag (Viewholder); }Else{Viewholder=(Viewholder) Convertview.gettag (); }                //Extracting Bean ObjectsItembean Bean =Listdate.get (position); //Setting Control DataViewHolder.img.setImageResource (BEAN.ITEMIMGRESID);        ViewHolder.textTitle.setText (Bean.itemtitle);        ViewHolder.textContent.setText (bean.itemcontent); returnConvertview; }        //Viewholder for caching    classviewholder{ PublicImageView img;  PublicTextView Texttitle;  PublicTextView textcontent; }}
View Code

This is basically the end of it. In the main page we call directly on the good. There are some details of the code, it is not sticking out. Should not affect everyone's understanding.

 Packagecom.imooc.baseadaptertest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.ListView;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); ListView ListView=(ListView) Findviewbyid (R.id.lv_main); List<ItemBean> dataList =NewArraylist<>(); //Create fake data         for(inti = 0; I < 20; i++) {Datalist.add (NewItembean (R.drawable.ic_launcher,"I am more <<<<<<" +I,"I am content, my word count is more" +i)); }        //setting up the adapterListview.setadapter (NewMyadapter ( This, dataList)); }}
View Code

Use of Android Baseadapter

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.