Android列表视图中的图像重复显示错误

时间:2016-06-07 09:54:57

标签: android listview

这是我的ListView适配器,图像重复出现,无法显示正确的图像。

public Alldogsdetails_adapter(Context context, ArrayList<HashMap<String, String>> value)
{
mcontext=context;
listname=value;

    // Memory Cache
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize)
    {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };
}


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

@Override
public Object getItem(int position) {
    return listname.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=View.inflate(mcontext, R.layout.test, null);
        holder = new ViewHolder();
        holder.district=(TextView)convertView.findViewById(R.id.District);
        holder.city=(TextView)convertView.findViewById(R.id.city);
        holder.serialno=(TextView)convertView.findViewById(R.id.serialno);
        holder.gender=(TextView)convertView.findViewById(R.id.gender);
        holder.dogimg=(ImageView)convertView.findViewById(R.id.dogimg);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    HashMap<String, String> result=listname.get(position);
    String dist=result.get("district");
    String city=result.get("city");
    String serialno=result.get("serialno");
    String gender=result.get("gender");
    final String imageLocation=result.get("before");
    if(holder.dogimg != null)
    {
        final Bitmap bitmap = getBitmapFromMemCache(imageLocation);
        if (bitmap != null)
        {
            holder.dogimg.setImageBitmap(bitmap);
        }
        else
        {
            new ImageDownloaderTask(holder.dogimg,imageLocation).execute();
        }
    }

    holder.district.setText(dist);
    holder.city.setText(city);
    holder.serialno.setText(serialno);
    holder.gender.setText(gender);

    return convertView;
}

public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}

public Bitmap getBitmapFromMemCache(String key) {
    return mMemoryCache.get(key);
}

static class ViewHolder
{
 TextView district, city, serialno, gender;
    ImageView dogimg;
}

class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap>
{
    private final WeakReference<ImageView> imageViewReference;
    String murl;
    ImageView showView;
    public ImageDownloaderTask(ImageView imageView,String url) {
        imageViewReference = new WeakReference<ImageView>(imageView);
        murl=url;
        showView=imageView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        final Bitmap bitmap=downloadBitmap(murl);
        if(murl!=null && bitmap!=null)
        {
            addBitmapToMemoryCache(murl, bitmap);
        }
        return bitmap;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
         ImageView imageView = imageViewReference.get();
            if (imageView != null) {


                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                    //addBitmapToMemoryCache(murl, bitmap);
                }

            }

        }
    }
}

private Bitmap downloadBitmap(String url)
{
    HttpURLConnection urlConnection = null;
    Bitmap bitmap;
    try {
        URL uri = new URL(url);
        urlConnection = (HttpURLConnection) uri.openConnection();
        int statusCode = urlConnection.getResponseCode();
        if (statusCode != HttpStatus.SC_OK)
        {
            return null;
        }
        InputStream inputStream = urlConnection.getInputStream();

        bitmap = BitmapFactory.decodeStream(inputStream);


        return bitmap;
    }

    catch (Exception e)
    {
        urlConnection.disconnect();
        Log.w("ImageDownloader", "Error downloading image from " + url);
    }
    finally
    {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return null;
}

}

0 个答案:

没有答案