Android ListView Shows Confusion problem

Source: Internet
Author: User

When using a custom ListView View, it is often used to improve slide and display efficiency by reusing the view in the ListView, but one problem is that the data in the ListView often shows confusion.

In general, the method we override is this:


Inflater = Layoutinflater.from (context); @Override public  View getView (int position, view Convertview, ViewGroup Parent) {    Viewholder holder;    if (Convertview = = null) {      Convertview = inflater.inflate (R.layout.book_item, null);      Holder = new Viewholder (convertview);      Book book = Books.get (position);      Holder.bookName.setText (Book.getname ());      Holder.bookTime.setText (Book.gettime ());      Convertview.settag (holder);    } else {      holder = (viewholder) convertview.gettag ();    }    return convertview;  }  Class Viewholder {    ImageView bookpic;    TextView BookName;    TextView Booktime;    Public Viewholder (view view) {      bookpic = (ImageView) View.findviewbyid (r.id.iv_book_pic);      BookName = (TextView) View.findviewbyid (r.id.tv_book_name);      Booktime = (TextView) View.findviewbyid (r.id.tv_book_time);    }  }



This usually causes the list to show confusion because we do not reload the data when we reuse the view but convertview! = null, as in this form the data is reloaded:

@Override public  View getView (int position, view Convertview, ViewGroup parent) {    Viewholder holder;    if (Convertview = = null) {      Convertview = inflater.inflate (R.layout.book_item, null);      Holder = new Viewholder (convertview);      Convertview.settag (holder);    } else {      holder = (viewholder) convertview.gettag ();    }    Book book = Books.get (position);//Reset Data    Holder.bookName.setText (Book.getname ()) regardless of whether the view is reused;    Holder.bookTime.setText (Book.gettime ());    return convertview;  }




Android ListView Shows Confusion problem

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.