与AsyncTask的ListView控件加载图片 [英] ListView Loading Images with AsyncTask

查看:143
本文介绍了与AsyncTask的ListView控件加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读通过大量的类似于煤矿问题的答案,但还是有问题,修复我的问题。我有一个项目,是一个RSS阅读器,在同一个的AsyncTask类的背景图像加载。除非用户滚动迅速然后将图像有时并不在我行加载该项目工程。他们从来没有加载不正确的地方,它只是好像他们被跳过,如果用户滚动快。此外,在启动时,只有2或1中的图像在我的列表视图负荷出的4行,该用户就可以看到。

我知道这个问题有什么做的,我用了WeakReference的对象,但我不知道如何以更好的方式实现它...

这是我RssListAdapter,其中包含我的异步类中。

 公共类RssListAdapter扩展ArrayAdapter<的JSONObject>
{
的TextView TextView的;
ImageView的ImageView的;
的JSONObject jsonImageText;
ProgressDialog progressDialog;
活动活性2;
查看rowView;

公共RssListAdapter(活动活动,列表和LT;的JSONObject> imageAndTexts)
{
    超级(活性,0,imageAndTexts);
    活性2 =活动;
}

@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{

    活动活性=(活动)的getContext();
    LayoutInflater充气= activity.getLayoutInflater();

    //从充气XML的意见
    查看rowView =(查看)充气
            .inflate(R.layout.image_text_layout,NULL);
    jsonImageText =的getItem(位置);

    // //////////////////////////////////////////////// ////////////////////////////////////////////////// //
    //接下来的章节中,我们在运行时更新文本 - 在规定
    // JSON从我们的REST调用
    // //////////////////////////////////////////////// //////////////////////////////////////////////////
    TextView的=(的TextView)rowView.findViewById(R.id.job_text);
    ImageView的=(ImageView的)rowView.findViewById(R.id.feed_image);

    BitmapDownloaderTask任务=新BitmapDownloaderTask();
    跨区文本;
    尝试
    {
        文=(跨区)jsonImageText.get(文字);
        textView.setText(文本);
    }
    赶上(JSONException E)
    {
        // TODO自动生成的catch块
        e.printStackTrace();
    }

    task.execute();

    返回rowView;

}

公共类BitmapDownloaderTask扩展的AsyncTask<字符串,太虚,位图>
{
    私人字符串URL;
    私人RssListAdapter适配器;
    私人的WeakReference< ImageView的> imageViewReference = NULL;

    @覆盖
    //实际的下载方法,在任务线程中运行
    受保护的位图doInBackground(字符串... PARAMS)
    {
        imageViewReference =新的WeakReference< ImageView的>(ImageView的);

        位图IMG = NULL;

        尝试
        {

            如果(jsonImageText.get(IMAGELINK)!= NULL)
            {

                的System.out.println(XXXX链接找到了!);
                字符串URL =(字符串)jsonImageText.get(IMAGELINK);
                URL feedImage =新的网址(URL);

                HttpURLConnection的康恩=(HttpURLConnection类)feedImage
                        .openConnection();
                InputStream的是= conn.getInputStream();
                IMG = BitmapFactory.de codeStream(是);
            }

        }
        赶上(MalformedURLException的E)
        {
            //处理异常在这里 - 如果无效的URL被解析
            //从RSS源项目
        }
        赶上(IOException异常E)
        {
            //处理异常在这里 - 也许没到Web访问
        }
        赶上(JSONException E)
        {
            // textView.setText(JSON异常);
        }
        返回IMG;
    }

    @覆盖
    //一旦图像被下载,它关联到的ImageView
    保护无效onPostExecute(位图位图)
    {
        如果(isCancelled())
        {
            位= NULL;
        }

        如果(imageViewReference!= NULL)
        {
            ImageView的ImageView的= imageViewReference.get();
            如果(ImageView的!= NULL)
            {
                imageView.setImageBitmap(位);
            }
        }

    }

    @覆盖
    //在图像加载
    在preExecute保护无效()
    {
        如果(imageViewReference == NULL)
        {
            imageView.setImageResource(R.drawable.stub);
        }
    }

}
}
 

解决方案

您应该检查的官方Android的显示位图有效地的教程就如何有效地加载和显示位图。它配备了一个准备使用一张code。

I have been reading through a lot of answers of questions that are similar to mine, but still having problem fixing my issue. I have a project that is an RSS Reader that loads in images in the background with an AsyncTask class. The program works, except if the user scrolls quickly then the images sometimes do not load in my rows. They never load in the incorrect spot, it just seems like they are skipped if the user scrolls quickly. Also, on start-up, only 2 or 1 of the images in my listview load out of the 4 rows that the user can see.

I know the problem has something to do with the WeakReference object that I use, but I am not sure how to implement it in a better way...

This is my RssListAdapter, which contains my Async class as well.

public class RssListAdapter extends ArrayAdapter<JSONObject>
{
TextView textView;
ImageView imageView;
JSONObject jsonImageText;
ProgressDialog progressDialog;
Activity activity2;
View rowView;

public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts)
{
    super(activity, 0, imageAndTexts);
    activity2 = activity;
}

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

    Activity activity = (Activity) getContext();
    LayoutInflater inflater = activity.getLayoutInflater();

    // Inflate the views from XML
    View rowView = (View) inflater
            .inflate(R.layout.image_text_layout, null);
    jsonImageText = getItem(position);

    // ////////////////////////////////////////////////////////////////////////////////////////////////////
    // The next section we update at runtime the text - as provided by the
    // JSON from our REST call
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    textView = (TextView) rowView.findViewById(R.id.job_text);
    imageView = (ImageView) rowView.findViewById(R.id.feed_image);

    BitmapDownloaderTask task = new BitmapDownloaderTask();
    Spanned text;
    try
    {
        text = (Spanned) jsonImageText.get("text");
        textView.setText(text);
    }
    catch (JSONException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    task.execute();

    return rowView;

}

public class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap>
{
    private String url;
    private RssListAdapter adapter;
    private WeakReference<ImageView> imageViewReference = null;

    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(String... params)
    {
        imageViewReference = new WeakReference<ImageView>(imageView);

        Bitmap img = null;

        try
        {

            if (jsonImageText.get("imageLink") != null)
            {

                System.out.println("XXXX Link found!");
                String url = (String) jsonImageText.get("imageLink");
                URL feedImage = new URL(url);

                HttpURLConnection conn = (HttpURLConnection) feedImage
                        .openConnection();
                InputStream is = conn.getInputStream();
                img = BitmapFactory.decodeStream(is);
            }

        }
        catch (MalformedURLException e)
        {
            // handle exception here - in case of invalid URL being parsed
            // from the RSS feed item
        }
        catch (IOException e)
        {
            // handle exception here - maybe no access to web
        }
        catch (JSONException e)
        {
            // textView.setText("JSON Exception");
        }
        return img;
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap)
    {
        if (isCancelled())
        {
            bitmap = null;
        }

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

    }

    @Override
    // Before images are loaded
    protected void onPreExecute()
    {
        if (imageViewReference == null)
        {
            imageView.setImageResource(R.drawable.stub);
        }
    }

}
}

解决方案

You should check the official Android "Displaying Bitmaps Efficiently" tutorial on how to load and display bitmaps efficiently. It comes with a ready to use piece of code.

这篇关于与AsyncTask的ListView控件加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆