An analysis of the pits in the Android adapter adapter

Source: Internet
Author: User

To do the project encountered, tortured me will be nearly two days, today to share the experience, so that we can not go a little detour, good, simple to say what is the Android adapter, how to define, how to increase the reusability of the adapter, how to reduce the program's coupling

Adapter as the name implies is used to do the adaptation, but he is how to adapt, the mechanism is what, the role of what, good, believe that the Java is to know what is object-oriented programming, here with the object-oriented interpretation is very good understanding, in fact, the adapter is to put each object in its space and then on the Andorid page display, Now to understand, the object is actually the abstract body of the data, and the page is generally the view of XML is the Views bar, then the data in the form of a view to display, then this is the role of adapter, and he can be in accordance with the corresponding sequence display, can be based on the corresponding operation of the data or page operation, So take a look at my adapter, on the code

This is the bean entity.

Package Com.example.fanyafeng.laugh.bean;import com.example.fanyafeng.laugh.util.s;/** * Created by Fanyafeng on 2015/ 6/30.    */public class Indexlistviewbean extends Basebean {private String Title;    Private String lefttopimg;    Private String Lefttoptitle;    Private String lefttoptimes;    Private String Lefttopurl;    Private String righttopimg;    Private String Righttoptitle;    Private String righttoptimes;    Private String Righttopurl;    Private String leftbottomimg;    Private String Leftbottomtitle;    Private String leftbottomtimes;    Private String Leftbottonurl;    Private String rightbottomimg;    Private String Rightbottomtitle;    Private String rightbottomtimes;    Private String Rightbottonurl; Public Indexlistviewbean (string title, String lefttopimg, String lefttoptitle, String lefttoptimes, String lefttopurl, St Ring righttopimg, String righttoptitle, String righttoptimes, String Righttopurl, String leftbottomimg, String Leftbottom Title, String LEFTBOTTOMTIMes, String leftbottonurl, String rightbottomimg, String rightbottomtitle, String rightbottomtimes, String        Rightbottonurl) {title = title;        lefttopimg = lefttopimg;        Lefttoptitle = Lefttoptitle;        Lefttoptimes = Lefttoptimes;        Lefttopurl = Lefttopurl;        righttopimg = righttopimg;        Righttoptitle = Righttoptitle;        Righttoptimes = Righttoptimes;        Righttopurl = Righttopurl;        leftbottomimg = leftbottomimg;        Leftbottomtitle = Leftbottomtitle;        Leftbottomtimes = Leftbottomtimes;        Leftbottonurl = Leftbottonurl;        rightbottomimg = rightbottomimg;        Rightbottomtitle = Rightbottomtitle;        Rightbottomtimes = Rightbottomtimes;    Rightbottonurl = Rightbottonurl;    } public String GetTitle () {return Title;    public void Settitle (String title) {title = title;    } public String getlefttopimg () {return lefttopimg;  } public void Setlefttopimg (String lefttopimg) {      lefttopimg = lefttopimg;    } public String Getlefttoptitle () {return lefttoptitle;    } public void Setlefttoptitle (String lefttoptitle) {lefttoptitle = Lefttoptitle;    } public String Getlefttoptimes () {return lefttoptimes;    } public void Setlefttoptimes (String lefttoptimes) {lefttoptimes = Lefttoptimes;    } public String Getlefttopurl () {return lefttopurl;    } public void Setlefttopurl (String lefttopurl) {lefttopurl = Lefttopurl;    } public String getrighttopimg () {return righttopimg;    } public void Setrighttopimg (String righttopimg) {righttopimg = righttopimg;    } public String Getrighttoptitle () {return righttoptitle;    } public void Setrighttoptitle (String righttoptitle) {righttoptitle = Righttoptitle;    } public String Getrighttoptimes () {return righttoptimes; } public void Setrighttoptimes (String righttoptimes) {righttoptimes =Righttoptimes;    } public String Getrighttopurl () {return righttopurl;    } public void Setrighttopurl (String righttopurl) {righttopurl = Righttopurl;    } public String getleftbottomimg () {return leftbottomimg;    } public void Setleftbottomimg (String leftbottomimg) {leftbottomimg = leftbottomimg;    } public String Getleftbottomtitle () {return leftbottomtitle;    } public void Setleftbottomtitle (String leftbottomtitle) {leftbottomtitle = Leftbottomtitle;    } public String Getleftbottomtimes () {return leftbottomtimes;    } public void Setleftbottomtimes (String leftbottomtimes) {leftbottomtimes = Leftbottomtimes;    } public String Getleftbottonurl () {return leftbottonurl;    } public void Setleftbottonurl (String leftbottonurl) {leftbottonurl = Leftbottonurl;    } public String getrightbottomimg () {return rightbottomimg; } public void Setrightbottomimg (String RIGHTBOTTOMIMG) {rightbottomimg = rightbottomimg;    } public String Getrightbottomtitle () {return rightbottomtitle;    } public void Setrightbottomtitle (String rightbottomtitle) {rightbottomtitle = Rightbottomtitle;    } public String Getrightbottomtimes () {return rightbottomtimes;    } public void Setrightbottomtimes (String rightbottomtimes) {rightbottomtimes = Rightbottomtimes;    } public String Getrightbottonurl () {return rightbottonurl;    } public void Setrightbottonurl (String rightbottonurl) {rightbottonurl = Rightbottonurl; } @Override Public String toString () {return "indexlistviewbean{" + "title= '" + Title + ' \ "                + ", lefttopimg= '" + lefttopimg + "\" + ", lefttoptitle= ' + lefttoptitle + ' + ' + ", lefttoptimes= '" + lefttoptimes + "\" + ", lefttopurl= ' + lefttopurl + ' \" + ", Righttopimg= ' "+righttopimg + ' \ ' + ', righttoptitle= ' + righttoptitle + ' \ ' + ', righttoptimes= ' "+ righ Ttoptimes + ' \ ' + ', righttopurl= ' + righttopurl + ' \ ' + ', leftbottomimg= ' "+ leftbottom IMG + ' \ ' + ', leftbottomtitle= ' + leftbottomtitle + ' \ ' + ', leftbottomtimes= ' + Leftbo Ttomtimes + ' \ ' + ', leftbottonurl= ' + leftbottonurl + ' \ ' + ', rightbottomimg= ' + right  bottomimg + ' \ ' + ', rightbottomtitle= ' + rightbottomtitle + ' \ ' + ', rightbottomtimes= ' "    + rightbottomtimes + ' + ' + ', rightbottonurl= ' + rightbottonurl + ' \ ' + '} '; }}

This is the adapter.

Package Com.example.fanyafeng.laugh.adapter;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.drawable.drawable;import Android.net.uri;import Android.support.v4.util.lrucache;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.Button; Import Android.widget.imageview;import Android.widget.listview;import Android.widget.textview;import Android.widget.toast;import Com.example.fanyafeng.laugh.r;import Com.example.fanyafeng.laugh.activity.videodisplayactivity;import Com.example.fanyafeng.laugh.activity.videolistactivity;import Com.example.fanyafeng.laugh.activity.videoviewplayingactivity;import Com.example.fanyafeng.laugh.bean.indexlistviewbean;import Com.example.fanyafeng.laugh.bean.indexurlbean;import Com.example.fanyafeng.laugh.util.imageloadercache;import Com.example.fanyafeng.laugh.util.l;import Com.exAmple.fanyafeng.laugh.util.listviewimagetaskutil;import Com.example.fanyafeng.laugh.util.stringtools;import Com.example.fanyafeng.laugh.util.syncimageloader;import java.util.list;/** * Created by Fanyafeng on 2015/7/1.    */public class Indexlistviewadapter extends Baseadapter {private context context;    Private list<indexlistviewbean> Indexlistviewbeans;    Private ListView ListView;    Cache to local SD card, and can update ListView picture private Imageloadercache Mimageloader;    Gets the maximum memory allocated by the current application private final int maxmemory = (int) runtime.getruntime (). MaxMemory ();    Use only one-fifth to make the picture cache private final int cacheSize = MAXMEMORY/5; Private lrucache<string, bitmap> Mlrucache = new lrucache<string, bitmap> (cacheSize) {//heavy Write sizeof () method @Override protected int sizeof (String key, Bitmap Bitmap) {//TODO auto-generated Me        Thod stub//How many KB are used here to calculate return bitmap.getrowbytes () * bitmap.getheight ()/1024;   }    }; Public Indexlistviewadapter (context context, list<indexlistviewbean> Indexlistviewbeans) {this.context = Co        ntext        This.indexlistviewbeans = Indexlistviewbeans;    Mimageloader = new Imageloadercache (context);    } public Imageloadercache Getimagerloader () {return mimageloader;    public void Update () {notifydatasetchanged ();    } @Override public int getcount () {return indexlistviewbeans.size ();    } @Override public Object getItem (int position) {return indexlistviewbeans.get (position);    } @Override public long getitemid (int position) {return position; } @Override public view GetView (final int position, view Convertview, final ViewGroup parent) {View view = C        Onvertview;        Viewholder holder = null;            if (view = = null) {view = Layoutinflater.from (context). Inflate (r.layout.item_fragment_homepage, NULL);            Holder = new Viewholder (); View.sEttag (holder); Holder.            Title = (TextView) View.findviewbyid (r.id.index_title); Holder.            Lefttopimg = (ImageView) View.findviewbyid (R.ID.INDEX_LEFT_UP_IV); Holder.            Lefttoptitle = (TextView) View.findviewbyid (R.ID.INDEX_LEFT_UP_TV); Holder.            Lefttoptimes = (TextView) View.findviewbyid (r.id.index_left_up_time); Holder.            Righttopimg = (ImageView) View.findviewbyid (R.ID.INDEX_RIGHT_UP_IV); Holder.            Righttoptitle = (TextView) View.findviewbyid (R.ID.INDEX_RIGHT_UP_TV); Holder.            Righttoptimes = (TextView) View.findviewbyid (r.id.index_right_up_time); Holder.            Leftbottomimg = (ImageView) View.findviewbyid (R.ID.INDEX_LEFT_DOWN_IV); Holder.            Leftbottomtitle = (TextView) View.findviewbyid (R.ID.INDEX_LEFT_DOWN_TV); Holder.            Leftbottomtimes = (TextView) View.findviewbyid (r.id.index_left_down_time); Holder.            Rightbottomimg = (ImageView) View.findviewbyid (R.ID.INDEX_RIGHT_DOWN_IV); Holder. RigHtbottomtitle = (TextView) View.findviewbyid (R.ID.INDEX_RIGHT_DOWN_TV); Holder.        Rightbottomtimes = (TextView) View.findviewbyid (r.id.index_right_down_time);        } else {holder = (Viewholder) view.gettag (); } holder.                Lefttopimg.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                Intent Intent = new Intent (Intent.action_call);                Intent.setclass (context, videoviewplayingactivity.class);                Intent.putextra ("Url_info", indexlistviewbeans.get (position). Getlefttopurl ());                L.D ("Lefttopurl", indexlistviewbeans.get (position). Getlefttopurl ());                L.D ("Click to get the location", position);            Context.startactivity (Intent);        }        }); Holder.                Righttopimg.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {  Intent Intent = new Intent (Intent.action_call);              Intent.setclass (context, videoviewplayingactivity.class);                Intent.putextra ("Url_info", indexlistviewbeans.get (position). Getrighttopurl ());            Context.startactivity (Intent);        }        }); Holder. Leftbottomimg.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)                {Intent Intent = new Intent (Intent.action_call);                Intent.setclass (context, videoviewplayingactivity.class);                Intent.putextra ("Url_info", indexlistviewbeans.get (position). Getleftbottonurl ());            Context.startactivity (Intent);        }        }); Holder.  Rightbottomimg.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)                {Intent Intent = new Intent (Intent.action_call);                Intent.setclass (context, videoviewplayingactivity.class); Intent.putextra ("Url_info", IndeXlistviewbeans.get (position). Getrightbottonurl ());            Context.startactivity (Intent);        }        }); Holder.        Title.settext (Indexlistviewbeans.get (position). GetTitle ()); Holder.        Lefttoptitle.settext (Indexlistviewbeans.get (position). Getlefttoptitle ()); Holder.        Lefttoptimes.settext (Indexlistviewbeans.get (position). Getlefttoptimes ()); Holder.        Righttoptitle.settext (Indexlistviewbeans.get (position). Getrighttoptitle ()); Holder.        Righttoptimes.settext (Indexlistviewbeans.get (position). Getrighttoptimes ()); Holder.        Leftbottomtitle.settext (Indexlistviewbeans.get (position). Getleftbottomtitle ()); Holder.        Leftbottomtimes.settext (Indexlistviewbeans.get (position). Getleftbottomtimes ()); Holder.        Rightbottomtitle.settext (Indexlistviewbeans.get (position). Getrightbottomtitle ()); Holder.        Rightbottomtimes.settext (Indexlistviewbeans.get (position). Getrightbottomtimes ()); LoadBitmap (Indexlistviewbeans.get (position). GETLEFTTOPIMG (), Holder.        LEFTTOPIMG); LoadBitmap (Indexlistviewbeans.get (position). GETRIGHTTOPIMG (), Holder.        RIGHTTOPIMG); LoadBitmap (Indexlistviewbeans.get (position). GETLEFTBOTTOMIMG (), Holder.        LEFTBOTTOMIMG); LoadBitmap (Indexlistviewbeans.get (position). GETRIGHTBOTTOMIMG (), Holder.        RIGHTBOTTOMIMG);    return view; } private void LoadBitmap (String urlstr, ImageView image) {Listviewimagetaskutil Asyncloader = new Listviewimag Etaskutil (image, Mlrucache);//An asynchronous Picture loading object Bitmap Bitmap = Asyncloader.getbitmapfrommemorycache (URLSTR);//first obtained from the memory cache            Take the picture if (bitmap! = null) {Image.setimagebitmap (bitmap);//If the picture is present in the cache, it is set directly to ImageView} else {  Image.setimageresource (r.drawable.wait);//Otherwise set the default picture Asyncloader.execute (URLSTR);//Then perform the asynchronous task Asycntask        To load pictures online}} static class Viewholder {TextView Title;        ImageView lefttopimg;        TextView Lefttoptitle;        TextView Lefttoptimes; ImageView Righttopimg        TextView Righttoptitle;        TextView Righttoptimes;        ImageView leftbottomimg;        TextView Leftbottomtitle;        TextView Leftbottomtimes;        ImageView rightbottomimg;        TextView Rightbottomtitle;    TextView Rightbottomtimes; }}
Focus on the writing of the entity and the writing of the adapter, then the definition of my entity is not only used to map the view with the data, but also it is important to set the data on the object, a single item to fetch, the original blogger does not have each object inside the four links corresponding to the Bean entity, Instead of using the global variables (the activity or fragment and adapter write together, this is not good, as for why I here a simple clubhouse, specific people to understand, the first is the code will be very messy, and then there is adapter reuse, There is a global variable more then one is not good to maintain a low efficiency, the rest of the real to understand the list of data, want to according to GetView in the position to find the corresponding link, but by grasping position I found that He does not and onitemonclick (this inside position is cumulative, and is the only) in the position like, this inside I set is each load three, and then pull up load more and then load three, so that, I print out of the position is 0 to 2 has been the death cycle, so the problem came, the corresponding link is definitely not, because it is in accordance with the GetView position to obtain, then, I think about the depth of the Android GetView mechanism is what, Why the data on the ListView control the corresponding relationship is not wrong, there is no confusion, this involves the interviewer often ask the interview questions, some will ask the ListView how to optimize, see some of the information is Settag, yes, this is, And it's the answer on the general web (but I think the answer is not very good, it's adapter how to handle the transaction, how to deal with it, this direction should be right), but this settag is precisely our problem, Because Google gives the ListView is if this view screen, then Google will give you to deal with the existing view if it is again, appear on the screen, then this time Settag began to work, in other words, if that, started to do else.

Bo Master processing time just started with a global list, but this one is not good, another did not achieve the effect, and then the blogger began to think of another method is Java object-oriented inside the set bean, good, the original blogger is in adapter and define the global variables, in the IF else, but onclickerlistener within the if else, so that, quite far from the blogger said above, the problem of the implementation of that, so that the screen slide and refresh the overall data inside the global will be messy, will be messy, this time, and then to dig deeper, To the above said that, is to be in the binding data there to listen to the event, and to get the corresponding data, so that you can get the desired effect, and the view of the display is doing so, no chaos

Here's how adapter maps data to XML, and in general the first thing to do is to write a holder, which is what the XML of the item needs, and then find the layout in this, and then find the corresponding ID, And then go and holder inside to correspond (the corresponding bean inside the definition, usually need to do the page display, and the background operation of the data is not to correspond to the corresponding layout id), and there is emphasis on the adapter inside some pits

GetView position is a fixed number of values, relative, do not go when global variables to use

Notice adapter Open an activity, and so on.

Listen to events do not write in the IFELSE structure, event processing is the same, put on the outside alone processing

Pay attention to the coupling, otherwise the code will be messy, according to the logic that the programmer's logical thinking should be very clear, there is always a way to allow you to decouple, but which is good, and suitable for your way of processing, this requires the experience of the programmer, the rest depends on everyone to experience, here is just an analysis

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

An analysis of the pits in the Android adapter adapter

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.