ListView自定义适配器丢失imageView

时间:2014-07-03 18:47:45

标签: android android-fragments android-listview imageview adapter

我有一个显示帖子列表的片段。列表中的项目是自定义的,并且有一个ImageView,我将从互联网上下载图像(通过URL)。

问题是下载的图像出错了(有时会在两个或多个项目中重复)。

package com.shoozy.app.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shoozy.app.R;
import com.shoozy.app.data.Post;

import java.util.ArrayList;

public class PostAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;

    ArrayList posts;

    public PostAdapter(Context context, ArrayList objects) {
        //super(context, textViewResourceId, objects);
        layoutInflater = LayoutInflater.from(context);
        this.posts = objects;
    }

    @Override
    public int getCount() {
        return posts.size();
    }

    @Override
    public Object getItem(int position) {
        return posts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_list_gallery, null);
            holder = new ViewHolder();
            holder.postIV = (ImageView) convertView.findViewById(R.id.iv_post);
            holder.userImageIv = (ImageView) convertView.findViewById(R.id.iv_user_post_gallery);

            holder.shopTV = (TextView) convertView.findViewById(R.id.tv_shop);
            holder.priceTV = (TextView) convertView.findViewById(R.id.tv_price);
            holder.usernameTV = (TextView) convertView.findViewById(R.id.tv_username);
            holder.numberLikesTV = (TextView) convertView.findViewById(R.id.tv_number_like);
            holder.numberCommentsTV = (TextView) convertView.findViewById(R.id.tv_number_comments);

            convertView.setTag(holder); // TODO: scoprire che cazzo vuol dire
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.userImageIv.setTag(position);
        holder.postIV.setTag(position);

        Post p = (Post) posts.get(position);

        holder.shopTV.setText(p.getShopName());
        // TODO: aggiungere la valuta
        holder.priceTV.setText(p.getPrice() + " €");
        holder.usernameTV.setText(p.getUser().getUsername());
        holder.numberLikesTV.setText(p.getLikesCount() + "");
        holder.numberCommentsTV.setText(p.getCommentCount() + "");

        if (holder.postIV != null) {
            new ImageDownloaderTask(holder.postIV).execute(p.getImageUrl());
        }
        if (holder.userImageIv != null) {
            // TODO: per l'immagine dell'utente vedere
            //new ImageDownloaderTask(holder.userImageIv).execute(p.getUser().getImageUrl());
        }

        return convertView;
    }

    static class ViewHolder {
        TextView shopTV;
        TextView priceTV;
        TextView usernameTV;
        TextView numberLikesTV;
        TextView numberCommentsTV;
        ImageView postIV;
        ImageView userImageIv;
    }
}

1 个答案:

答案 0 :(得分:0)

我建议您使用AQuery's image loading

我的代码可以正常使用JQuery的图像加载

public class FriendListAdapter extends BaseAdapter {

private static class ViewHolder {
    public final RelativeLayout rootView;
    public final ImageView ivFriendAvatar;
    public final TextView tvFriendName;
    public final ImageView ivOnlineStatus;

    private ViewHolder(RelativeLayout rootView, ImageView ivFriendAvatar,
            TextView tvFriendName, ImageView ivOnlineStatus) {
        this.rootView = rootView;
        this.ivFriendAvatar = ivFriendAvatar;
        this.tvFriendName = tvFriendName;
        this.ivOnlineStatus = ivOnlineStatus;
    }

    public static ViewHolder create(RelativeLayout rootView) {
        ImageView ivFriendAvatar = (ImageView) rootView
                .findViewById(R.id.iv_friend_avatar);
        TextView tvFriendName = (TextView) rootView
                .findViewById(R.id.tv_friend_name);
        ImageView ivOnlineStatus = (ImageView) rootView
                .findViewById(R.id.iv_online_status);
        return new ViewHolder(rootView, ivFriendAvatar, tvFriendName,
                ivOnlineStatus);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.item_lv_friend_list,
                parent, false);
        vh = ViewHolder.create((RelativeLayout) convertView);
        convertView.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }

    AQuery aq = listAq.recycle(convertView);
    Bitmap placeHolder = aq.getCachedImage(R.drawable.anochat_ic_stub);

    Profile item = data.get(position);

    try {
        isLoadDelay = aq.shouldDelay(position, convertView, parent,
                item.getRealAvatar().getThumbnailURL());
    } catch (Exception e) {
        isLoadDelay = false;
    }

    if (isLoadDelay) {
        aq.id(vh.ivFriendAvatar).image(placeHolder);
    } else {
        if (item.getRealAvatar() == null) {
            aq.id(vh.ivFriendAvatar).image(R.drawable.anochat_ic_avatar);
        } else {
            aq.id(vh.ivFriendAvatar).image(item.getRealAvatar().getThumbnailURL(), true, true, 0,
                    R.drawable.anochat_ic_stub);
        }
    }

    if (item.getOnlineStatus() == OnlineStatus.ONLINE.ordinal()) {
        vh.ivOnlineStatus.setImageResource(R.drawable.anochat_ic_online);
    } else {
        vh.ivOnlineStatus.setImageResource(R.drawable.anochat_ic_offline);
    }

    if (item.getRealName() == null || item.getRealName().trim().length() == 0) {
        vh.tvFriendName.setText(item.getUserName());
    } else {
        vh.tvFriendName.setText(item.getRealName());
    }

    return convertView;
}

private LayoutInflater mInflater;
private Context context;
private List<Profile> data = new ArrayList<Profile>();
private AQuery listAq;
private boolean isLoadDelay;

// Constructors
public FriendListAdapter(Context context, List<Profile> data, AQuery listAq) {
    super();
    this.mInflater = LayoutInflater.from(context);
    this.context = context;
    this.data = data;
    this.listAq = listAq;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}