Strange behaviour in Android listview when loading images with asynctask

戏子无情 提交于 2019-12-06 12:53:33

Actually main problem is you didn't add else condition statement in most of your if condition statements in getView() method of custom adapter.

Like,

  1. put else here

    if (chatContact.Picture != null) {
            BitmapWorkerTask bitmapWorker = new BitmapWorkerTask(contactImageView, diskImageLoader);
            bitmapWorker.execute(chatContact.Picture);
    }
    else
    {
            contactImageView.setImageResource(R.id.icon); // only for your reference.. add default image
    } 
    
  2. Add else statement in your all if-else if statement for list item row views. Either make them gone in visibility or assign blank text or default images.

Update: As I doubt this scenario because of reference view of list row.

remove this code,

 View view = convertView;

 if (view == null) {
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     view = inflater.inflate(R.layout.chat_contact_list_row, null);
 }

Just use,

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.chat_contact_list_row, null);

I just spent several hours trying to figure out why this was happening for my list view when I was asynchronously loading images for each row. It turns out that the list view has to have "match_parent" for the width and height set in the XML. Truly strange and weird bug.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!