Android error: Incorrect ListView loading _ incorrect ListView image,
When the ListView loads an item, the images in multiple items will be misplaced.
For example, the pictures of the same person are messy.
I found a figure to explain the cause of the problem.
The problem is that convertView is reused. When convertView is reused, seven records are displayed on the first screen, and getView is called seven times. Seven convertviews are created,
When Item1 draws a screen and Item8 enters the screen, no new view instance is created for Item8. Item8 reuse the view created at Item1,
In Item8, the data of Item8 is refreshed, but the code is written in this way at the beginning. The intention is not to load the Avatar when it is empty, which can save resources.
String avatarUrl = user.getAvatar();if (avatarUrl!=null&&!avatarUrl.equals("")) {ImageLoader.getInstance().displayImage(avatarUrl, viewHolder.userLogo,ActivityUtil.getOptions(R.drawable.user_icon_default_main));}
If the avatar of item8 is an empty string, no value is assigned. As a result, item8 displays the avatar of item1, and so on, the original convertView image is displayed, which leads to misplacement of the image.
So I changed it to the following:
// Avatar String avatarUrl = null; if (user. getAvatar ()! = Null) {avatarUrl = user. getAvatar ();} ImageLoader. getInstance (). displayImage (avatarUrl, viewHolder. userLogo, ActivityUtil. getOptions (R. drawable. user_icon_default_main ));
Regardless of whether the Avatar is not empty, load it. Replace the original image of the reused convertView.
Author: jason0539
Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)
We recommend that you scan the QR code to follow the public account and look at different things.