加载列表视图具有多个异步任务的图像

时间:2014-11-11 12:57:34

标签: android android-listview android-asynctask lazy-loading android-volley

我要求在列表视图中显示图像,当页面加载时,我将从网络调用的JSON列表中获取file_name,以形成文件名的url格式。我需要执行另一个网络调用,将文件从s3服务器下载到本地服务器。我正在使用凌空的图像加载器如何实现这就是我正在做的方式

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    Log.d(TAG, "getView called.. ");
    ViewHolder holder = null;
    AllMessage message = amList.get(position);
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.list_peer_message, parent, false);
        holder.name = (TextView) convertView.findViewById(R.id.list_am_name_top);
        holder.sub_name = (TextView) convertView.findViewById(R.id.list_am_name_bottom);
        holder.content = (TextView) convertView.findViewById(R.id.list_am_content);
        holder.time = (TextView) convertView.findViewById(R.id.list_am_time);
        holder.img_icon = (NetworkImageViewRounded) convertView.findViewById(R.id.imageView1);
        holder.img_clip = (ImageView) convertView.findViewById(R.id.list_attachment_clip);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (message.getType().equalsIgnoreCase("group")) {

        holder.img_icon.setDefaultImageResId(R.drawable.group_icon);

        //holder.img_icon.setDefaultImageResId(R.drawable.group_icon);
        holder.name.setText(message.getGroupName());
        holder.name.setTag("group");
        holder.sub_name.setText(message.getName() != null ? message.getName() + ":" : "");
        holder.sub_name.setTag(message.getGroupID());
        holder.img_icon.setTag(message.getGroupAvatarUrl());
        if (message.getGroupAvatarUrl().equals("")) {
            //holder.img_icon.setImageUrl(null,imageLoader);
            holder.img_icon.setDefaultImageResId(R.drawable.group_icon);
            holder.img_icon.setImageUrl(null,imageLoader);
            holder.img_icon.setTag("");

        } else {
            if (message.getGroupAvatarUrl().contains("http")) {
                // holder.img_icon.setDefaultImageResId(R.drawable.group_icon);
                holder.img_icon.setTag(message.getGroupAvatarUrl());
                holder.img_icon.setImageUrl(message.getGroupAvatarUrl(),imageLoader);
                holder.img_icon.setDefaultImageResId(R.drawable.group_icon);

            } else {
                holder.img_icon.setTag(message.getGroupAvatarUrl());
                holder.img_icon.setImageUrl(FileUtil.getServerAttachmentPath() + "/" + message.getAvatarUrl(),
                            imageLoader);
                new DownLoadAttachmentsAsyncTask().execute(holder.img_icon);

                holder.img_icon.setDefaultImageResId(R.drawable.group_icon);


            }


        }

    } else {
        holder.img_icon.setDefaultImageResId(R.drawable.ic_doctor_default);
        holder.name.setText(message.getName());
        holder.name.setTag("individual");
        holder.sub_name.setText("");
        holder.sub_name.setTag(message.docID);
        holder.img_icon.setTag(message.getAvatarUrl());

        if (message.getAvatarUrl().equals("")) {
            Log.d(TAG, "Empty Doc avathar for " + message.getName());
            holder.img_icon.setDefaultImageResId(R.drawable.ic_doctor_default);
            holder.img_icon.setImageUrl(null,imageLoader);
            //holder.img_icon.setImageUrl(null,imageLoader);

            //holder.img_icon.setImageResource(R.drawable.ic_doctor_default);
            //holder.img_icon.setImageResource(R.drawable.ic_doctor_default);
        } else {

            holder.img_icon.setImageUrl(FileUtil.getServerAttachmentPath() + "/" + message.getAvatarUrl(),
                        imageLoader);
            new DownLoadAttachmentsAsyncTask().execute(holder.img_icon);

            holder.img_icon.setDefaultImageResId(R.drawable.ic_doctor_default);
            //holder.img_icon.setErrorImageResId(R.drawable.ic_doctor_default);
            //holder.img_icon.setImageUrl(FileUtil.getServerAttachmentPath() + "/" + message.getAvatarUrl(),imageLoader);

        }

    }



    return convertView;
}

private static class ViewHolder {
    TextView name, sub_name, content, time;
    NetworkImageViewRounded img_icon;
    ImageView img_clip;
}


 // DownLoad Async task for avathars 
public class DownLoadAttachmentsAsyncTask extends AsyncTask<NetworkImageViewRounded, Void, NetworkImageViewRounded> {
    //private final WeakReference<NetworkImageViewRounded> imageViewReference;
    //private final WeakReference<Integer> position;

    public DownLoadAttachmentsAsyncTask() {
        //imageViewReference = new WeakReference<NetworkImageViewRounded>(imageViewReference);
    }

    @Override
    protected void onCancelled() {
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected NetworkImageViewRounded doInBackground(NetworkImageViewRounded... params) {
        NetworkImageViewRounded  imageView = null;
        if (NetworkUtil.isNetworkAvailable(mContext)) {
            imageView = params[0];
            // Log.d("doInBackground",fileName);
            if (APIEngine.downLoadAttachment(imageView.getTag().toString(), "avatar")) {
                Log.d(TAG, "Downlaod done for name filename is "+imageView.getTag().toString());
                return imageView;

            } else {
                Log.d(TAG, "Downlaod failed for   --- "+imageView.getTag().toString());
            }

        } else {
            Log.d(TAG, "No network ");
        }
        return imageView;
    }

    @Override
    protected void onPostExecute(NetworkImageViewRounded imageView) {
        if (imageView != null) {

            if (imageView.getTag().toString() != null) {
                String fileUrl = imageView.getTag().toString();
                /*String fileNameJust;
                if(fileUrl.contains("http")){
                    fileNameJust = FileUtil.getFileNameFromS3URL(fileUrl);
                }else{
                    fileNameJust = fileUrl;
                }*/
                Log.d(TAG, FileUtil.getServerAttachmentPath() + "/" + fileUrl);
                imageView.setImageUrl(FileUtil.getServerAttachmentPath() + "/" + fileUrl,
                            imageLoader);

            }else{
                Log.d(TAG, "Nul file URL");
            }
            //notifyDataSetChanged();

        }

    }

}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

无需使用ImageLoader / Universal Imageloader Library。结帐This名为Picasso的真棒图书馆。下载jar文件here。在项目的构建路径中包含jar,它将使用Url通过简单的方法将图像加载到您的imageview中

    Picasso.with(context).load("imageurl.png").into(yourImageView);

只需在适配器的getview方法中使用此行。