Android-listview uses adapter more efficiently

Source: Internet
Author: User

Android -- listview uses adapter more efficiently

I. Adapter

The adapter is the intermediary between the listview and the data source.

When each piece of data enters the visible area, the adapter calls its getview () method and returns a view representing the specific data. Frequently called during scrolling, supporting thousands of data.

① The simplest method, the slowest and least practical

public void getView(int position , View convertView , ViewGroup parent){        View item = mInflater.inflate(R.layout.list_view, null);        (TextView)item.findViewById(R.id.text).setText(DATA[position]);        (ImageView)item.findViewById(R.id.img).setImageBitmap(icon);        return item;    }

② Recycling views using convertview improves the efficiency by 200%

public void getView(int position , View convertView , ViewGroup parent){        if(convertView == null){            convertView = mInflater.inflate(R.layout.list_view, null);        }      (TextView)convertView.findViewById(R.id.text).setText(DATA[position]);
      (ImageView)convertView.findViewById(R.id.img).setImageBitmap(icon);
         return convertView;    }  

③ Use the viewholder mode to Increase the efficiency by 50%

static class ViewHolder{        private TextView tv;        private ImageView iv;    }        public void getView(int position , View convertView , ViewGroup parent){        ViewHolder holder;        if(convertView == null){            convertView = mInflater.inflate(R.layout.list_view, null);            holder = new ViewHolder();            holder.tv = (TextView)convertView.findViewById(R.id.text);            holder.iv = (ImageView)convertView.findViewById(R.id.img);            convertView.setTag(holder);        }else{            holder = (ViewHolder) convertView.getTag();        }        holder.tv.setText(DATA[position]);        holder.iv.setImageBitmap(icon);        return convertView;    }

Comparison of update efficiency:

Related Article

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.