我有一个应用程序,我在其中下载并在线程中显示图像。一切正常。但是当图像被加载时,相同的图像被加载到两到三个位置,并且当我们滚动屏幕图像时加载正确。请问你们中的任何人都可以对此提出任何建议吗?
代码:
try{
holder.progress.setVisibility(View.VISIBLE);
DownLoadImageInAThreadHandler(Item, holder);
}
catch(Exception e)
{
System.out.println("Exception in Downloading image : " + e.getMessage());
}
return convertView;
}
//This code is outside the getview method
public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{
nImageDownLoads++;
System.out.println("The images being downloaded :" + nImageDownLoads);
final Handler handler = new Handler()
{
@Override public void handleMessage(Message message)
{
holder.imgitem.setImageDrawable((Drawable) message.obj);
holder.imgitem.setVisibility(View.VISIBLE);
holder.progress.setVisibility(View.GONE);
}
};
//Thread for downloading the images
Thread t = new Thread()
{
public void run()
{
try
{
Item.bImageDownLoaded = 2;
System.out.println("Downloading image :"+Item.ImageUrl);
drawable=getDrawableFromUrl(Item.ImageUrl);
nImageDownLoads--;
System.out.println("Downloaded image :" + Item.ImageUrl);
System.out.println("Remaining images for downloading: " + nImageDownLoads);
if(drawable != null)
{
Item.bImageDownLoaded = 1;
//Send the message to the handler
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
else{
int idNoImage = R.drawable.giftsuggestionsnoimage;
Drawable dwgNoImg = getParent().getResources().getDrawable(idNoImage);
//Send the message to the handler
Message message = handler.obtainMessage(1, dwgNoImg);
handler.sendMessage(message);
}
}
catch(Exception exp)
{
System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
}
}
private Drawable getDrawableFromUrl(String imageUrl) throws IOException
{
try
{
image = DownloadDrawable(imageUrl, "src");
//bmp = readBitmapFromNetwork(new URL(imageUrl));
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
//return bmp;
}
};
t.start();
}
Drawable DownloadDrawable(String url, String src_name) throws java.io.IOException
{
return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), src_name);
}