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 7730707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:17:35+00:00 2026-06-01T06:17:35+00:00

I have a ListActivity and in it I set my list items with a

  • 0

I have a ListActivity and in it I set my list items with a class that extends SimpleCursorAdapter. I’m overriding bindView to set my Views. I have some TextViews and ImageViews. This is how I set my list items in my cursor adapter:

String variableName = "drawable/q" + num + "_200px";
int imageResource = context.getResources().getIdentifier(variableName, "drawable", context.getPackageName());
if (imageResource != 0 ) {
    // The drawable exists
    Bitmap b = BitmapFactory.decodeResource(context.getResources(), imageResource);
    width = b.getWidth();
    height = b.getHeight();
    imageView.getLayoutParams().width = (int) (width);
    imageView.getLayoutParams().height = (int) (height);
    imageView.setImageResource(imageResource);
} else {
    imageView.setImageResource(R.drawable.25trans_200px);
}

The problem I’m having is whenever I update my list with setListAdapter I get a large amount of garbage collection:

D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 125K, 51% free 2710K/5447K, external 2022K/2137K, paused 75ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 30K, 51% free 2701K/5447K, external 2669K/2972K, paused 64ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 23K, 51% free 2713K/5447K, external 3479K/3579K, paused 53ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 22K, 51% free 2706K/5447K, external 3303K/3352K, paused 64ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 21K, 51% free 2722K/5447K, external 3569K/3685K, paused 102ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 23K, 50% free 2755K/5447K, external 3499K/3605K, paused 65ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 23K, 50% free 2771K/5447K, external 4213K/4488K, paused 53ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 18K, 49% free 2796K/5447K, external 5057K/5343K, paused 75ms
D/dalvikvm(18637): GC_EXTERNAL_ALLOC freed 28K, 49% free 2803K/5447K, external 5944K/5976K, paused 53ms
D/dalvikvm(  435): GC_EXPLICIT freed 6K, 54% free 2544K/5511K, external 1625K/2137K, paused 50ms
D/dalvikvm(  165): GC_EXPLICIT freed 85K, 52% free 2946K/6087K, external 4838K/5980K, paused 111ms
D/dalvikvm(  448): GC_EXPLICIT freed 1K, 54% free 2540K/5511K, external 1625K/2137K, paused 50ms
D/dalvikvm(  294): GC_EXPLICIT freed 8K, 55% free 2598K/5703K, external 1625K/2137K, paused 64ms

What can I do to avoid this? It’s causing my UI to be sluggish and when I scroll, too.

  • 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-01T06:17:36+00:00Added an answer on June 1, 2026 at 6:17 am
    1. You don’t need to load the Bitmap just to get the length and width. You probably want to look at ImageView’s scaleType property. That will adjust the bounds of the ImageView according to the image being loaded in.
    2. Your code implies that you have multiple image states based on a number. You might want to look at the LevelListDrawable XML for that. Basically you can set a list of drawables that are selected by the number you pass in to them. If you assign the LevelListDrawable to the ImageView in XML or in code. All you’ll probably have to do is call imageView.setImageLevel(num).

    bindView is a critical method when animating a list. It gets called when the ListView is scrolled and new items are loaded in because they are appearing. bindView gets called on the UI thread which means animation cannot continue until it finishes. Usually this isn’t a problem because you’re just assigning variables. In this code you’re calling Bitmap b = BitmapFactory.decodeResource(context.getResources(), imageResource); which has to hit the NAND flash which is expensive. You can use StrictMode to detect these kinds of problems by the way.

    Bitmaps do get unloaded from memory when no longer used but it’s not immediate, the GC has to get called. Android offers a way to drop the large byte array data that bitmaps carry with the Bitmap.recycle() method. That will hopefully solve the OutOfMemoryError's.

    If the approaches above don’t work for some reason then you’ll have to deal with a more complex solution which involves threads and callbacks.

    Let me know if you need help with that. If you can please clarify why you need the width and height of the image?

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

Sidebar

Related Questions

I have one android activity which extends ListActivity. Every time list items is pressed
I have a set of views that I need to present in a list-like
I have a class defined as public class viewGroups extends ListActivity Somewhere in the
Currently I have a class that is extending the ListActivity class. I need to
I have a ListView that has some minor visual preferences that are set in
I have a listactivity where I read/set diferent controls such as textviews, buttons or
I have a class that is already extending TabActivity so i can't extend ListActivity.
I have following code: public class ShowActivity extends ListActivity implements OnItemClickListener { /** Called
I have created a custom ListActivity that shows a list of skills and buttons
I have a contextMenu set up and it shows items from the list. However,

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.