An analysis of the pits in the Android adapter adapter

Source: Internet
Author: User

Doing the project encountered, tortured me will be nearly two days, today to share the experience. Let's take a little detour later, OK. Simply say what is the Android adapter and how to define it. How to add the reusability of the adapter. How to reduce the coupling of the program

The adapter, by definition, is used for adaptation, but how does it fit. What the mechanism is. What the role is, good, believe that writing Java is to know what is object-oriented programming bar, here with object-oriented to explain very good understanding. In fact, the adapter is to put each object in its space and then on the Andorid page display, and now to understand, the object is actually the abstract body of data, and the page display is usually XML is the view bar, then the data is displayed in the form of a view, then this is the role of adapter, And he can make it appear in the corresponding order, can be based on the corresponding operation to the data or page operation, then look at my adapter, 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 be able to update the ListView picture Private Imageloadercache Mimageloader;    Gets the maximum memory allocated by the current application private final int maxmemory = (int) runtime.getruntime (). MaxMemory ();    Only one of the five points used to do 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 =        Context        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 loaded object Bitmap Bitmap = Asyncloader.getbitmapfrommemorycache (URLSTR);//first obtained from the memory cache            Take the picture if (bitmap! = null) {Image.setimagebitmap (bitmap);//Assuming that the picture exists in the cache is set directly to ImageView} else {  Image.setimageresource (r.drawable.wait);//Otherwise set the default picture Asyncloader.execute (URLSTR);//Then run 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, so now my entity's definition is not only used to map the view with data, another very important thing is to set the data on the object, a single item to fetch, the original blogger does not have four links within each object corresponding bean entity, Instead of using global variables (write activity or fragment and adapter together, it's not good to write.) As for why I here a simple clubhouse, detailed everyone 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 really have to understand the list data, Want to GetView in accordance with the position to find the corresponding link, but by grasping position I found that he does not and Onitemonclick (the position in this is in turn cumulative. And is the only one) in the same position. In this case I set the load of three, and then pull up a lot of other load three. In this case, I printed the position is 0 to 2 of the dead loop, so the problem comes, the corresponding link is definitely not correct. Because it is in accordance with the GetView position to obtain, then, I think deeply. Android GetView mechanism is what, why the corresponding relationship between the data on the ListView control 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 said is Settag. Right. This is, and is the answer on the general network (but I feel that the answer to the optimization is not very good, is how to deal with the transaction in adapter, how to deal with, this direction should be correct). But this settag is precisely our problem, because Google gives the ListView is assumed this view after the screen. Then Google will handle it for you. The view hypothesis of this existence is again drawn back. On the screen now, then Settag started to work, in other words, if. Start running else.

Bo Master processing time just started with a global list, but such a is not good, there is not to achieve the effect, and then the blogger began to think there is another way is Java object-oriented inside the set bean, good, the original Bo is in adapter and define the global variables, in the IF Other than the else obtained. But onclickerlistener in the if else, that kind of thing, quite far from the one the blogger says, runs that problem. This kind of words to the screen of the swipe and refresh the overall data inside the world will certainly be chaotic, will be messy. This time To dig deeper, on the above said that, is to bind the data there to do event monitoring, and to obtain the corresponding data, such words can get the desired effect. And that's what the view shows. No mess.

Let's talk about how adapter maps data to XML, and generally writes a holder first. There are all the things that are required for XML in the item, and then find the layout in this, then find the corresponding ID. And then go to the holder inside the corresponding (defined in the corresponding bean. It is usually necessary to display the page, and the background operation of the data is not to go to the corresponding layout id), and here the 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 of the program Ape's logical thinking should be very clear. There is always a way to get you to decouple. But which one is good. And it suits your way of handling. This requires the experience of the program ape, the rest depends on everyone to experience, here is simply an analysis

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.