Android Simple development of the general adapter Viewholder

Source: Internet
Author: User

Our usual way of using adapter

public class Busbasesearchapadter extends Simplebaseapadter {private list<busbase> busbases = new arraylist< Busbase> ();p ublic Context context;public void setlist (list<busbase> busbases) {this.busbases = busbases;} Public Busbasesearchapadter (Context context) {super (context); this.context = context;} @Overridepublic int GetCount () {return busbases.size ();} @Overridepublic Object getItem (int position) {return busbases.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Busbase busbase = busbases.get (position); Viewholder holder = null;if (Convertview = = null) {holder = new Viewholder (); Convertview = Layoutinflater.from (context). Inflate (R.layout.hx_item_search_bus, parent, false); holder.hx_item_bus_search_image = (ImageView) Convertview.findviewbyid (r.id.hx_item_bus_search_image); holder.hx_item_bus_search_name = (TextView) Convertview.findviewbyid (r.id.hx_item_bus_search_name); holder.hx_item_bus_search_mobile = (TextView) Convertview.findviewbyid (r.id.hx_item_bus_search_mobile); Convertview.settag (holder);} Else{holder = (viewholder) convertview. Gettag ();} Bitmaputils.display (Holder.hx_item_bus_search_image, Busbase.getshoplogo ()); Holder.hx_item_bus_search_ Name.settext (Busbase.name); Holder.hx_item_bus_search_mobile.setText (busbase.mobile); return convertview;} Class Viewholder{imageview Hx_item_bus_search_image; TextView Hx_item_bus_search_name; TextView Hx_item_bus_search_mobile;}


Can see that the practical code only have GetView and Viewholder but other code and have to write, too painful to have to repeatedly write countless adapter and Viewhoder.


We can write a generic viewholder.

public class Viewholder {private final sparsearray<view> mviews;      private int mposition;      Private View Mconvertview;      protected Bitmaputils bitmaputils; Private Viewholder (context context, viewgroup parent, int layoutid, int position) {Bitmaputils = n EW bitmaputils (context); Bitmaputils.configdefaultloadingimage (r.drawable.hx_loodding); Bitmaputils.configdefaultloadfailedimage (r.drawable.hx_loodding); Bitmaputils.configdefaultbitmapconfig (        Bitmap.Config.RGB_565);          This.mposition = position;          This.mviews = new sparsearray<view> ();          Mconvertview = Layoutinflater.from (context). Inflate (LayoutID, parent, false);      Settag Mconvertview.settag (this); }/** * Got a Viewholder Object * * @param context * @param convertview * @param parent * @ Param LayoutID * @param position * @return * * public static Viewholder Get (context context, VieW Convertview, viewgroup parent, int layoutid, int position) {if (Convertview = = null)          {return new Viewholder (context, parent, layoutid, position);      } return (Viewholder) Convertview.gettag ();      Public View Getconvertview () {return mconvertview; /** * Gets the control by the ID of the control, assuming no add views * * @param viewId * @return */public <t Exte          NDS view> T getView (int viewId) {View view = Mviews.get (viewId);              if (view = = null) {view = Mconvertview.findviewbyid (viewId);          Mviews.put (viewId, view);      } return (T) view; /** * Set string for TextView * * @param viewId * @param text * @return */Public View          Holder setText (int viewId, String text) {TextView view = GetView (viewId);          View.settext (text);      return this;       } /** * Set Picture for ImageView * * @param viewId * @param drawableid * @return */Public viewho          Lder setimageresource (int viewId, int drawableid) {ImageView view = GetView (viewId);            View.setimageresource (Drawableid);      return this; /** * Set Picture for ImageView * * @param viewId * @param drawableid * @return */Publi          C viewholder setimagebitmap (int viewId, Bitmap BM) {ImageView view = GetView (viewId);          View.setimagebitmap (BM);      return this; /** * Set Picture for ImageView * * @param viewId * @param drawableid * @return */Publi        C viewholder setimagebyurl (int viewId, String url) {bitmaputils.display ((ImageView) GetView (viewId), URL);      return this;      } public int getPosition () {return mposition; }

Use aSparsearray Save the view.

Now adapter writes like this

Public abstract class Abstuctbaseapadter<t> extends Simplebaseapadter {protected Layoutinflater minflater;  protected Context Mcontext;  protected list<t> Mdatas;  protected int Mitemlayoutid;      Public Abstuctbaseapadter (Context context) {super (context);}        Public Abstuctbaseapadter (context context, list<t> Mdatas, int. Itemlayoutid) {This (context);          This.mcontext = context;          This.minflater = Layoutinflater.from (Mcontext);          This.mdatas = Mdatas;      This.mitemlayoutid = Itemlayoutid;      } public void setlist (list<t> mdatas) {this.mdatas = Mdatas;      } @Override public int getcount () {return mdatas.size ();      } @Override public T getItem (int position) {return mdatas.get (position);      } @Override public long getitemid (int position) {return position; } @Override public view getView (int position, view Convertview, ViewGroup parent) {final Viewholder Viewholder = Getviewholder (position, Convertview, PA          Rent);          Convert (Viewholder, GetItem (position));        return Viewholder.getconvertview ();        } public abstract void convert (Viewholder helper, T item); Private Viewholder getviewholder (int position, View Convertview, ViewGroup parent) {return Vie      Wholder.get (Mcontext, Convertview, parent, mitemlayoutid, position); }



Now we just need to rewrite the Convert method to see how the activity is used

Abstuctbaseapadter abstuctbaseapadter = new Abstuctbaseapadter<map<string,object>> (Activity,list_map, R.layout.hx_money_item) {public void convert (Viewholder helper, map<string, object> item) {Helper.settext ( R.id.id_item_1, Item.get ("Time"). ToString ()); Helper.settext (R.id.id_item_3, Item.get ("SourceName"). ToString ());}};



Writing anonymous internal classes directly in the activity is possible.

Haha job changed to

Android Simple development of the general adapter Viewholder

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.