ListView Loading Images with AsyncTask

北城以北 提交于 2020-01-01 21:52:30

问题


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

}
}

回答1:


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.



来源:https://stackoverflow.com/questions/13687002/listview-loading-images-with-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!