スレッドに画像をダウンロードして表示するアプリがあります。すべてが正常に機能しています。しかし、画像が読み込まれると、同じ画像が 2 ~ 3 か所に読み込まれ、スクロールすると画面の画像が正しく読み込まれます。あなたの誰かがこれに何か提案をすることができますか?
コード:
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);
}