ListView Data Dislocation problem

Source: Internet
Author: User

Today colleagues do a send voice function, found that when clicking on the voice playback, the actual voice of the speaker is not the click of that, but jumped to another place, his code as follows

Public View GetView (int position, View Convertview, ViewGroup parent) {chatmsgentity entity = coll.get (position); Boolean I Scommsg = Entity.getmsgtype (); Layoutinflater Minflater = (layoutinflater) context. Getsystemservice (Context.layout_inflater_service); Viewholder viewholder=null;if (Convertview = = null) {if (iscommsg) {Convertview = Minflater.inflate (R.layout.chatting_ Item_msg_text_left, null);} else {Convertview = minflater.inflate (r.layout.chatting_item_msg_text_right, null);} Viewholder = new Viewholder (); viewholder.tvsendtime = (TextView) Convertview.findviewbyid (r.id.tv_sendtime); Viewholder.tvusername = (TextView) Convertview.findviewbyid (r.id.tv_username); viewholder.tvcontent = (TextView) Convertview.findviewbyid (r.id.tv_chatcontent); Viewholder.icon = (ImageView) Convertview.findviewbyid (R.id.iv_ Userhead); viewholder.chatpic = (ImageView) Convertview.findviewbyid (r.id.chat_img); viewholder.iscommsg = ISCOMMSG; Convertview.settag (Viewholder);} else {Viewholder = (ViewHolder) Convertview.gettag ();} Final String mass = Entity.getmessage (), Onclicklistener listener = new View.onclicklistener () {@Overridepublic void oncli CK (View arg0) {//TODO auto-generated method Stubif (Mass.contains ("/storage/") &&mass.endswith (". jpg")) {}else if (Mass.contains ("/storage/") &&mass.endswith (". Amr")) {if (Judgeanmi) {Ad.start (); judgeanmi =!judgeanmi;} else {ad.stop (); judgeanmi =!judgeanmi;}}}; ViewHolder.tvSendTime.setText (Entity.getdate ()); ViewHolder.tvUserName.setText (Entity.getname ()); if ( Mass.contains ("/storage/") &&mass.endswith (". jpg")) {Bitmap BM = Converttobitmap (Entity.getmessage (), 200,200); bitmapdrawable bd = new bitmapdrawable (BM); viewHolder.chatPic.setBackgroundDrawable (BD); ViewHolder.tvContent.setText ("");} else if (Mass.contains ("/storage/") &&mass.endswith (". Amr")) {ViewHolder.chatPic.setBackgroundResource ( R.anim.voice_muti); ad = (animationdrawable) viewHolder.chatPic.getBackground (); ViewHolder.tvContent.setText ( Mass.substring (0, Mass.indexof ("/")) + "″");} else {viewHolder.tvContent.setText (mass); viewHolder.chatPic.setBackgroundDrawable (null);} ViewHolder.chatPic.setOnClickListener (listener); return convertview;}
first look at Google's official demo of the wording

  Public View GetView (int. position, View Convertview, ViewGroup parent) {//A Viewholder keeps references to C            Hildren views to avoid unneccessary calls//to Findviewbyid () on each row.            Viewholder Holder; When convertview are not NULL, we can reuse it directly, there are no need//to reinflate it.            We only inflate a new View when the Convertview supplied//by ListView is null.                if (Convertview = = null) {Convertview = minflater.inflate (R.layout.list_item_icon_text, NULL);                Creates a viewholder and store references to the other children views//We want to bind data to.                Holder = new Viewholder ();                Holder.text = (TextView) Convertview.findviewbyid (R.id.text);                Holder.icon = (ImageView) Convertview.findviewbyid (R.id.icon);            Convertview.settag (holder); } else {//Get the Viewholder BACK to get fast access to the TextView//and the ImageView.            Holder = (viewholder) convertview.gettag ();            }//Bind the data efficiently with the holder.            Holder.text.setText (Data[position]);            Holder.icon.setImageBitmap ((position & 1) = = 1? micon1:micon2);        return convertview;            } static class Viewholder {TextView text;        ImageView icon; }    }


Later he resolved the problem by changing the definition of Viewholder to final viewholder.

Probably think about it, because Android to the ListView cache optimization, in fact, the memory only retains a screen of view, that is, the parameters inside the Convertview, and before the Viewholder is in the Convertview is empty when new out, and was Convertview Settag go in, so, the Viewhold objects that are taken out behind are cached. But for each holder inside the Chatpic to set a click event Listener. So, it's just that the listener is set repeatedly for those holder objects that are cached. However, there is no problem with the final modification of the holder object, because the final modified reference does not change the object that he points to. Then why is it that I can not think clearly, to see the great god detailed.

ListView Data Dislocation 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.