Latest Version: optimized listview adapter for Android Development

Source: Internet
Author: User

We all knowAndroidMediumAdapterThe role isListviewThe bridge between the interface and data. When each item in the list is displayed on the pageAdapterOfGetviewMethod returnsView. Ever wondered? In our list, we have1000000 itemsWhat will happen? Does it occupy a lot of system resources?

Let's take a look at the following code:

Java code:

  1. Copy to clipboardJava code
    1. Public View getview (INT position, view convertview, viewgroup parent ){
    2. View Item = minflater. Inflate (R. layout. list_item_icon_text, null );
    3. (Textview) item. findviewbyid (R. Id. Text). settext (data [position]);
    4. (Imageview) item. findviewbyid (R. Id. Icon). setimagebitmap (
    5. (Position & 1) = 1? Micon1: micon2 );
    6. Return item;
    7. }

You don't need to tell me what's going on. If there are more than 1000000 items, the consequences are unimaginable! You must never write this!

Let's take a look at the following code:

Java code:

Copy to clipboardJava code
  1. Public View getview (INT position, view convertview, viewgroup parent ){
  2. If (convertview = NULL ){
  3. Convertview = minflater. Inflate (R. layout. Item, null );
  4. }
  5. (Textview) convertview. findviewbyid (R. Id. Text). settext (data [position]);
  6. (Imageview) convertview. findviewbyid (R. Id. Icon). setimagebitmap (
  7. (Position & 1) = 1? Micon1: micon2 );
  8. Return convertview;
  9. }

How is the above Code much better? The system will reduce the numberView. The performance has been greatly improved.

Are there any optimization methods? The answer is yes: Let's take a look at the following code!

Java code:

Copy to clipboardJava code
  1. Public View getview (INT position, view convertview, viewgroup parent ){
  2. Viewholder holder;
  3. If (convertview = NULL ){
  4. Convertview = minflater. Inflate (R. layout. list_item_icon_text, null );
  5. Holder = new viewholder ();
  6. Holder. Text = (textview) convertview. findviewbyid (R. Id. Text );
  7. Holder. Icon = (imageview) convertview. findviewbyid (R. Id. Icon );
  8. Convertview. settag (holder );
  9. } Else {
  10. Holder = (viewholder) convertview. gettag ();
  11. }
  12. Holder. Text. settext (data [position]);
  13. Holder. Icon. setimagebitmap (Position & 1) = 1? Micon1: micon2 );
  14. Return convertview;
  15. }
  16. Static class viewholder {
  17. Textview text;
  18. Imageview icon;
  19. }

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.