Universal Adapter for Android development

Source: Internet
Author: User

Many things like ListView, GridView, and so on need adapters. And if you develop an app every ListView has to write a adapter, then how happy to play the game.

What is the use of Viewholider and why?

The blogger writes very well.

Http://www.cnblogs.com/lichenwei/p/4085107.html

The so-called universal adapter is simply extracting the adapter's duplicated code to encapsulate it. Different functions of the code is left to write a method for the user to replicate, then each of the ListView adapter will only become a few words is enough.

This is the code for the generic adapter:

Private class Myadapter extends Baseadapter {private Context mcontext=null;                Private Layoutinflater Inflater;            Public Myadapter (Context c) {this.mcontext=c;            This.inflater=layoutinflater.from (c);//from tells the system where to get the layout filler} @Override public int getcount () {        TODO auto-generated method stub return mlisttitle.length; } @Override public Object getItem (int arg0) {//TODO auto-generated method stub retur        n arg0; } @Override public long getitemid (int arg0) {//TODO auto-generated method stub retur        n arg0; } @Override public View getView (int arg0, view arg1, ViewGroup arg2) {//TODO auto-generated met            Hod stub viewholder Viewholder =null;                if (arg1==null) {viewholder=new viewholder (); Arg1=inflater.inflate (R.layout.icOnlist, arg2, false);                viewholder.iv= (ImageView) Arg1.findviewbyid (R.id.imageview);                Viewholder.title= (TextView) Arg1.findviewbyid (r.id.title);                viewholder.text= (TextView) Arg1.findviewbyid (R.id.text);            Arg1.settag (Viewholder);            } else {viewholder= (viewholder) Arg1.gettag ();            } viewholder.iv.setImageResource (R.drawable.icon1);            Viewholder.title.setText (mlisttitle[arg0]);            Viewholder.text.setText (mliststr[arg0]);        return arg1;        }} private class Viewholder {private ImageView iv=null;        Private TextView Title=null;    Private TextView Text=null; }

First, we'll encapsulate the Viewholder:

1. Viewholder converts the XML file to Convertview and returns the converted View

2. To set the label

3, judge whether Convertview is empty, empty words re-created, not empty words gettag ();

Code:

1  PackageCom.example.list_view;2 ImportAndroid.content.Context;3 ImportAndroid.util.SparseArray;4 ImportAndroid.view.LayoutInflater;5 ImportAndroid.view.View;6 ImportAndroid.view.ViewGroup;7 8  Public classEasyviewholder {9 Ten     PrivateSparsearray<view>Sparsearray; One     Privatecontext context; A     Private intLayoutID; -     PrivateView Convertview; -      PublicEasyviewholder (Context Mcontext,view Mconvertview,intlayoutid,viewgroup Parent) the     { -          This. context=Mcontext; -          This. sparsearray=NewSparsearray<view>(); -          This. Convertview=layoutinflater.from (Mcontext). Inflate (LayoutID, parent,false); +Convertview.settag ( This);  -     } +      Public StaticEasyviewholder Get (Context context,view Convertview,intlayoutid,viewgroup Parent) A     { at         if(convertview==NULL) -         { -             return NewEasyviewholder (context, Convertview, LayoutID, parent); -         } -          -          in             //It is important to note that because the ListView is reused, such as the screen only displays 5 item, then the 6th will be reused with the 1th item, so this side needs to be updated position -             return(Easyviewholder) Convertview.gettag (); to          +     } -      Public<textendsView> T GetView (intVIEWID)//component found by ID the     { *View view =Sparsearray.get (viewId); $         if(view==NULL)Panax Notoginseng         { -view= (View) This. Convertview.findviewbyid (viewId); the Sparsearray.put (viewId, view); +         } A         return(T) view; the          +     } -      PublicView Getconvertview () $     { $         return  This. Convertview; -     } -}

To encapsulate an adapter:

Declares an abstract class

1, keep the same method

2, modify GetView ()

3, write a need to rewrite the function for the user to pass value.

1  PackageCom.example.list_view;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 ImportAndroid.content.Context;7 ImportAndroid.view.LayoutInflater;8 ImportAndroid.view.View;9 ImportAndroid.view.ViewGroup;Ten ImportAndroid.widget.BaseAdapter; One  A  Public Abstract classEasyadapter<t>extendsBaseadapter { -  -     Privatecontext context; the     PrivateArraylist<t>data; -     PrivateLayoutinflater Inflater; -     Private intLayoutID; - Easyviewholder Easyholder; +      PublicEasyadapter (Context context,arraylist<t> data,intLayoutID) -     { +          This. context=context; A          This. data=data; at Inflater.from (context); -          This. Layoutid=LayoutID; -     } - @Override -      Public intGetCount () { -         //TODO auto-generated Method Stub in         returndata.size (); -     } to  + @Override -      PublicObject GetItem (intarg0) { the         //TODO auto-generated Method Stub *         returnarg0; $     }Panax Notoginseng  - @Override the      Public LongGetitemid (intarg0) { +         //TODO auto-generated Method Stub A         returnarg0; the     } +  - @Override $      PublicView GetView (intarg0, View arg1, ViewGroup arg2) { $         //TODO auto-generated Method Stub -         FinalEasyviewholder easyholder=Getholder (context, arg1, LayoutID, arg2); -Setconverview (Easyholder,data.get (arg0));//requires user replication to transmit value the         returnEasyholder.getconvertview (); -     }Wuyi       Public Abstract voidSetconverview (Easyviewholder easyviewholder, T t); the       Public StaticEasyviewholder Getholder (Context context,view Convertview,intlayoutid,viewgroup Parent) -     { Wu             returneasyviewholder.get (context, Convertview, LayoutID, parent); -     } About}

The above two classes are not changed in addition to adding new features.

Next, we can complete the adapter with minimal code:

1  PackageCom.example.list_view;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 ImportAndroid.content.Context;7 ImportAndroid.widget.ImageView;8 ImportAndroid.widget.TextView;9 Ten  Public classMyadapterextendsEasyadapter<user> { One  A      PublicMyadapter (context context, arraylist<user> data,intLayoutID) { -         Super(context, data, layoutid); -         //TODO auto-generated Constructor stub the     } -Just pass in the following function the value you need -      Public voidSetconverview (Easyviewholder myviewholder, User t) { -         //TODO auto-generated Method Stub +          - ((ImageView) Myviewholder.getview (R.id.imageview)). Setimageresource (R.drawable.icon1); + ((TextView) Myviewholder.getview (R.id.title)). SetText (T.gettitle ()); A ((TextView) Myviewholder.getview (R.id.text)). SetText (T.gettext ()); at          -     } -  -}

Mainactivity's Code:

for (int i=0;i<10;i++)
{
User User=new user ();
User.setiv (R.drawable.icon1);
User.settitle ("user" +i);
User.settext ("+i");
List.add (user);
}
Myadapter adapter=new myadapter (mainactivity.this, List, r.layout.iconlist);
List1.setadapter (adapter);

Simply passing in the data sources you need for this listview, and the layout of the incoming ListView, you can complete a variety of ListView.

Universal Adapter for Android development

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.