0

我有一个应用程序,我在其中下载并在线程中显示图像。一切正常。但是当图像被加载时,相同的图像被加载到两到三个位置,并且当我们滚动屏幕图像时加载正确。请问你们中的任何人都可以对此提出任何建议吗?

代码:

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);
}
4

2 回答 2

0

你在使用 ListView 吗?如果是这样,当您在其中写入图像时,该视图是否可能已经被 ListView 回收(重新用于显示另一个列表项)?

于 2012-04-13T12:14:50.057 回答
0

发生问题是因为您已经编写了在getView()方法中加载图像的代码。

每次滚动列表时都会调用该方法。

所以这是一个糟糕的想法,根本不是好主意。

从那里删除该代码..加载图像并在其他threadAsyncTask 然后调用notifyDataSetChanged()您的Adapter class对象。

只是我指示要做的步骤,请仔细阅读并尝试实施,如果无法理解任何内容,请随时询问..抱歉无法粘贴任何代码片段..

ListView 适配器的 getView() 方法

show default icon if array doesn't contain bitmap at that position
if bitmap is there show bitmap else show default icon

AsyncTask to Load Data

do in background
load data and show in listview

AsyncTask to Load Images

do in background
download images one bye one from the urls got in first AsyncTask
in onPostExecute call adapter's notifyDataSetChanged() to show the bitmaps too

you can also call onProgressUpdate periodically means..after 2-3 image download 
call publishProgress in doInBackground which will call onProgressUpdate and 
in that method also write adpater.notifyDataSetChanged() to 
reflect currently downloaded/available bitmaps in array
于 2012-04-13T12:15:44.187 回答