Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:58:12+00:00 2026-06-12T04:58:12+00:00

For various Android applications, I need large ListView s, i.e. such views with 100-300

  • 0

For various Android applications, I need large ListViews, i.e. such views with 100-300 entries.

All entries must be loaded in bulk when the application is started, as some sorting and processing is necessary and the application cannot know which items to display first, otherwise.

So far, I’ve been loading the images for all items in bulk as well, which are then saved in an ArrayList<CustomType> together with the rest of the data for each entry.

But of course, this is not a good practice, as you’re very likely to have an OutOfMemoryException then: The references to all images in the ArrayList prevent the garbage collector from working.

So the best solution is, obviously, to load only the text data in bulk whereas the images are then loaded as needed, right? The Google Play application does this, for example: You can see that images are loaded as you scroll to them, i.e. they are probably loaded in the adapter’s getView() method. But with Google Play, this is a different problem, anyway, as the images must be loaded from the Internet, which is not the case for me. My problem is not that loading the images takes too long, but storing them requires too much memory.

So what should I do with the images? Load in getView(), when they are really needed? Would make scrolling sluggish. So calling an AsyncTask then? Or just a normal Thread? Parametrize it?

I could save the images that are already loaded into a HashMap<String,Bitmap>, so that they don’t need to be loaded again in getView(). But if this is done, you have the memory problem again: The HashMap stores references to all images, so in the end, you could have the OutOfMemoryException again.

I know that there are already lots of questions here that discuss “Lazy loading” of images. But they mainly cover the problem of slow loading, not too much memory consumption.

Edit: I’ve now decided to start AsyncTasks in getView() which load the image into the ListView in the background. But this causes my application to run into an RejectedExecutionException. What should I do now?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:58:13+00:00Added an answer on June 12, 2026 at 4:58 am

    I took the approach of loading the images with an AsyncTask and attaching the task to the view in the adapter’s getView function to keep track of which task is loading in which view. I use this in an app of mine and there’s no scroll lag and all images are loaded in the proper position with no exceptions being thrown. Also, because the task does no work if it’s canceled, you can perform a fling on your list and it should lag up at all.

    The task:

    public class DecodeTask extends AsyncTask<String, Void, Bitmap> {
    
    private static int MaxTextureSize = 2048; /* True for most devices. */
    
    public ImageView v;
    
    public DecodeTask(ImageView iv) {
        v = iv;
    }
    
    protected Bitmap doInBackground(String... params) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPurgeable = true;
        opt.inPreferQualityOverSpeed = false;
        opt.inSampleSize = 0;
    
        Bitmap bitmap = null;
        if(isCancelled()) {
            return bitmap;
        }
    
        opt.inJustDecodeBounds = true;
        do {
            opt.inSampleSize++;
            BitmapFactory.decodeFile(params[0], opt);
        } while(opt.outHeight > MaxTextureSize || opt.outWidth > MaxTextureSize)
        opt.inJustDecodeBounds = false;
    
        bitmap = BitmapFactory.decodeFile(params[0], opt);
        return bitmap;
    }
    
    @Override
    protected void onPostExecute(Bitmap result) {
        if(v != null) {
            v.setImageBitmap(result);
        }
    }
    
    }
    

    The adapter stores an ArrayList that contains the file paths of all the images that need loaded. The getView function looks like this:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView iv = null;
        if(convertView == null) {
            convertView = getLayoutInflater().inflate(R.id.your_view, null); /* Inflate your view here */
            iv = convertView.findViewById(R.id.your_image_view);
        } else {
            iv = convertView.findViewById(R.id.your_image_view);
            DecodeTask task = (DecodeTask)iv.getTag(R.id.your_image_view);
            if(task != null) {
                task.cancel(true);
            }
        }
        iv.setImageBitmap(null);
        DecodeTask task = new DecodeTask(iv);
        task.execute(getItem(position) /* File path to image */);
        iv.setTag(R.id.your_image_view, task);
    
        return convertView;
    }
    

    NOTE: Just a caveat here, this might still give you memory problems on versions 1.5 – 2.3 since they use a thread pool for AsyncTask. 3.0+ go back to the serial model by default for executing AsyncTasks which keeps it to one task running at a time, thus using less memory at any given time. So long as your images aren’t too big though, you should be fine.

    UPDATE: While this solution will still work, there have been great additions to the open source community for solving this problem in a cleaner way. Libraries like Glide or Picasso both handle loading items in a list quite well and I’d recommend you look into one of those solutions if possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an Android application and I need to handle various touch events.
In my Android application I need to read a large amount of data from
From observing source code for various Android applications (not written by me), I noticed
In various Android applications, I use the following code to show an application chooser
Long back, android used to have GtalkService that let applications use Gtalk for various
How do you implement a fixed view through various activities in Android 2.1 upwards?
I am trying to learn Fragments in Android and from various examples I have
I am trying to fetch various email addresses from contact list of android mobile
Various native applications in Windows Phone 7 (Hotmail, Gmail, etc.) seem to be able
For various reasons, I need to runOnUiThread() the actual instantiation & initialization of WebView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.