Android Basics Getting Started tutorial--2.4.5 ListView checkbox Dislocation Problem Solving

Source: Internet
Author: User

Android Basics Getting Started tutorial--2.4.5 ListView checkbox Dislocation Problem Solving

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

As one of the classic problems with the ListView, if you've tried customizing the ListView item with a checkbox on it, then
This problem occurs when you have more than one page of item, so let's analyze the cause of this problem and how to
Solve this problem!

1. Why the problem occurred:

This is a picture of the web. A diagram of the method invocation mechanism of the ListView GetView

There is a recycler , usually the item visible on our ListView is in memory, and his item is placed in
In this recycler, the first time the item is loaded, the Convertview in the current page is null, and when you scroll out of the screen, this is the time
Convertview is not empty, so the new one will re-use this convertview!
We can write a simple example, follow the log, here are some of the log chart after the run!

See, Postion starting from 12, Convertview is not empty, specifically what this represents,
I do not know, visual to go source ... Well, we know that Convertview will cache here, that's why.
caused by the checkbox dislocation, so the first solution is to not reuse this convertview, or
Say this convertview is set to null each time GetView, but if you need to display a large number of item,
This method will appear very bloated, generally practical development we are using the following workaround:
Find a thing to save the status of the current item checkbox, initialize the time to judge, set whether or not selected!

2. Workaround Example:

There are a lot of ways to store this checkbox and you can put it in a hashmap

 Public  class  person implements Serializable{    PrivateString name;PrivateString number;Private BooleanCheckStatus; Public  Person(string name, string number) {Super(); This. name = name; This. Number = number; This. CheckStatus =false; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringGetNumber() {returnNumber } Public void Setnumber(String number) { This. Number = number; } Public Boolean Getcheckstatus() {returnCheckStatus; } Public void Setcheckstatus(BooleanCheckStatus) { This. checkstatus = CheckStatus; }}

Implementation of the adapter class:Contactlistadapter.java:

 Public  class contactlistadapter extends baseadapter {    PrivateList<person> Mdata;PrivateContext Mcontext; Public Contactlistadapter(list<person> data, context context)        {mdata = data;    Mcontext = context; }//Define a method to refresh data     Public void Changedata(list<person> data)        {mdata = data;    Notifydatasetchanged (); }@Override     Public int GetCount() {returnMdata.size (); }@Override     PublicObjectGetItem(intPosition) {returnMdata.get (position); }@Override     Public Long Getitemid(intPosition) {returnPosition }@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Final intindex = position; Viewholder Viewholder;if(Convertview = =NULL{Convertview = Layoutinflater.from (Mcontext). Inflate (r.layout.item_contact, parent,false); Viewholder =NewViewholder ();            viewholder.ly = (relativelayout) convertview. Findviewbyid (R.id.lycontactlistitem);            Viewholder.txtname = (TextView) convertview. Findviewbyid (R.id.txtname);            Viewholder.txtnumber = (TextView) convertview. Findviewbyid (R.id.txtnumber);            Viewholder.cbxstatus = (CheckBox) convertview. Findviewbyid (R.id.cbxstatus);        Convertview.settag (Viewholder); }Else{Viewholder = (Viewholder) convertview.gettag (); } viewholder.cbxstatus. Setoncheckedchangelistener (NewOncheckedchangelistener () {@Override                     Public void oncheckedchanged(Compoundbutton Buttonview,BooleanisChecked) {if(isChecked) mdata.get (index). Setcheckstatus (true);ElseMdata.get (Index). Setcheckstatus (false); }                });if(Mdata.get (position). getcheckstatus () = =true) viewHolder.cbxStatus.setChecked (true);ElseViewHolder.cbxStatus.setChecked (false);        ViewHolder.txtName.setText (Mdata.get (index). GetName ()); ViewHolder.txtNumber.setText (Mdata.get (index). GetNumber ());returnConvertview; }Private  class viewholder {Relativelayout ly;        TextView txtname;        TextView Txtnumber;    CheckBox Cbxstatus; }}

Hey, very simple, and don't forget a bit:
The checkbox listener's method is added before the code that initializes the checkbox state.

Introduction to this section:

OK, this section explains a classic problem of the ListView, the checkbox in the ListView is misplaced
Problem solving, simply add a record checkbox to select the status value, and then rewrite the checkbox
Click on the event, the first to make a judgment ~ Thank you ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--2.4.5 ListView checkbox Dislocation Problem Solving

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.