Android developed adapter-listview adapter for duplicate data

Source: Internet
Author: User

An adapter is a bridge between data in Android and view view, which is the function of displaying data through the adapter to the corresponding view view.

At work, when using a ListView to do adapter data, some people must have met, how to optimize efficiency, but also the situation of duplication of data, how to avoid duplication of data and can improve the size of the ListView large data efficiency? , the solution is 2 points:

1. In the GetView method of view judgment, that is, to do convertview ==null such a judgment, this is to improve the efficiency of large data.

2. Set the data to use the Settag ()/gettag () method to access the data, so as to prevent duplication of data display

Through the above two-point method of processing, you can achieve a better display efficiency, but also to prevent repeated display of data problems. Here is an example of the implementation:

The main core solution is part of the code that is highlighted in red.

 PackageCom.czm.adapter;Importjava.util.ArrayList;ImportAndroid. R.integer;ImportAndroid.content.Context;ImportAndroid.util.Log;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView;ImportCom.czm.entity.FileItem;ImportCOM.CZM.XCFILEMANAGER.R;/***//Set the SD card list adapter *@authorJczmdeveloper **/ Public classSdcardlistadapterextendsbaseadapter{PrivateLayoutinflater Inflater; Privatecontext Context; PrivateArrayList ListData; Private Boolean[] checkflaglist; Static classlistitemview{//Custom Control Collections         PublicImageView ImageView;  PublicTextView Tvfilename;  PublicTextView Tvfileother;  PublicImageView Ivcheck; }           Publicsdcardlistadapter (Context ctx,arraylist list) { This. Context =CTX;  This. ListData =list; Inflater=Layoutinflater.from (context); Checkflaglist=New Boolean[Listdata.size ()]; } @Override Public intGetCount () {//TODO auto-generated Method Stub        returnlistdata.size (); } @Override PublicObject GetItem (intposition) {        //TODO auto-generated Method Stub        return NULL; } @Override Public LongGetitemid (intposition) {        //TODO auto-generated Method Stub        return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {        //TODO auto-generated Method Stub//Custom view Listitemview Listitemview = null; if (Convertview ==null) {//Get view of list_item layout file Convertview = inflater.inflate (r.layout.sdcard_list_ite                        m, null);            Listitemview = new Listitemview ();            Gets the control object Listitemview.imageview = (ImageView) Convertview.findviewbyid (R.id.ivfilelogo);            Listitemview.tvfilename = (TextView) Convertview.findviewbyid (r.id.tvfilename);            Listitemview.tvfileother = (TextView) Convertview.findviewbyid (R.id.tvfileother);                        Listitemview.ivcheck = (ImageView) Convertview.findviewbyid (R.id.ivcheck);        Sets the control set to Convertview Convertview.settag (Listitemview);        }else {Listitemview = (Listitemview) convertview.gettag (); }        //set up text and picturesFileitem Fileitem =(Fileitem) listdata.get (position);        ListItemView.imageView.setImageResource (Fileitem.getimageid ());        ListItemView.tvFileName.setText (Fileitem.getstrfilename (). toString ()); ListItemView.tvFileOther.setText (Fileitem.getnfilesize ()+fileitem.getstrdatetime (). toString ()); if(Checkflaglist[position]) {ListItemView.ivCheck.setImageResource (R.drawable.check); }Else{listItemView.ivCheck.setImageResource (R.drawable.uncheck); }                        returnConvertview; }     Public voidSetcheckflag (intPositionBooleanbchecked) {Checkflaglist[position]=bchecked; }     Public BooleanGetcheckflag (intposition) {        returnCheckflaglist[position]; }     Public Boolean[] Getcheckflags () {returncheckflaglist; }}

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.