android - ViewHolder ImageView随机加载

时间:2014-08-31 00:55:56

标签: android json listview imageview

我是Android开发的初学者。我尝试使用JSON创建ListView。我有一个随机加载的ImageView的问题。这是我的MoviesAdapter.java:

    package com.dianagung.json;

    import java.io.InputStream;
    import java.util.ArrayList;

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

public class MoviesAdapter extends ArrayAdapter<Movies> {

    ArrayList<Movies> ArrayListMovies;
    int Resource;
    Context context;
    LayoutInflater vi;
    ViewHolder holder;


    public MoviesAdapter(Context context, int resource,
            ArrayList<Movies> objects) {
        super(context, resource, objects);
        ArrayListMovies = objects;
        Resource = resource;
        this.context = context;

        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();

            v = vi.inflate(Resource, null);
            holder.mImage = (ImageView) v.findViewById(R.id.mImage);
            holder.mTitle = (TextView) v.findViewById(R.id.mTitle);
            holder.mDate = (TextView) v.findViewById(R.id.mDate);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.mImage.setImageResource(R.drawable.ic_launcher);
        new DownloadImage(holder.mImage).execute(ArrayListMovies.get(position)
                .getImage());
        holder.mTitle.setText(ArrayListMovies.get(position).getTitle());
        holder.mDate.setText(ArrayListMovies.get(position).getDate());
        return v;
    }

    static class ViewHolder  {
        public ImageView mImage;
        public TextView mTitle;
        public TextView mDate;
    }

    // Download Image Task
    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImage(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon;
        }

        protected void onPostExecute(Bitmap result) {
            if (result != null) {
                Bitmap bmp2 = Bitmap.createScaledBitmap(result, 80, 80, true);
                bmImage.setImageBitmap(bmp2);
            }
        }

    }

}

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

在致电holder.mImage.setImageBitmap(null);之前添加此行new DownloadImage(holder.mImage).execute(ArrayListMovies.get(position) .getImage());,即可完成工作。

顺便说一下,如果有大量的位图,你可能没有使用正确的方法来解码可能遇到OutOfMemory Exceptions的位图,你可以使用NOSTRA13's library进行异步图像加载。