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

  • Home
  • SEARCH
  • 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 6249893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:19:22+00:00 2026-05-24T13:19:22+00:00

I have a ListView that has one image and two lines of texts for

  • 0

I have a ListView that has one image and two lines of texts for each element (organized by a RelativeLayout). It works ok, but it’s too slow and I know where the problem comes from!

This is the getView() method for the custom adapter that I’m using:

public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false);
                mViewHolder = new ViewHolder();
                mViewHolder.cover = (ImageView) convertView.findViewById(R.id.app_icon);
                mViewHolder.title = (TextView) convertView.findViewById(R.id.selection);
                mViewHolder.description = (TextView) convertView.findViewById(R.id.app_short_description);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }

            // Here is the origin of the issue ! 
            final Feed currentFeed = getItem(position);
            mViewHolder.title.setText(currentFeed.getTitle());
            mViewHolder.description.setText(currentFeed.getDescription());

            try {
                if(currentFeed.getThumbnailUrl() != null) {
                     downloadThumbnail(mViewHolder.cover, currentFeed.getThumbnailUrl());
                }
            } catch(Exception e) {
                e.printStackTrace();
            }

            return convertView;
}

private static class ViewHolder {
        TextView title;
        TextView description;
        ImageView cover;
}

So I have done some manual benchmarking and it appears that allocating an instance of Feed is the source of this slowness:

final Feed currentFeed = getItem(position);

I know this because I have written another version of this to compare the two:

// Here is the origin of the issue ! 
            //final Feed currentFeed = getItem(position);
            mViewHolder.title.setText("Title");
            mViewHolder.description.setText("Description");

            try {
            if(currentFeed.getThumbnailUrl() != null) {
                 downloadThumbnail(mViewHolder.cover, "some url");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

This one was way smoother (even with the downloadThumbnail() method working).

I also precise that there are only 15 items on my ListView.

I know that allocating objects is very expensive because of garbage collection but I can’t any other way to do it!

Any idea?

Thanks!

EDIT

Don’t mind too much about the downloadThumbnail() method, it already does some caching. And actually even without any picture, it’s still slow.

  • 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-05-24T13:19:22+00:00Added an answer on May 24, 2026 at 1:19 pm

    When user scrolls the list, getView gets called on the adapter. Make sure that you dont do same things repeatedly, for example generating thumbnail. If number of items is limited (for example video content), then you can create all views and keep it ready for get view. Otherwise you may have to implement cacheing.

    Below code shows an adapter and listView implementation, where in all listviews are created and stored in memory. Since this is meant for video browsing, memory does not pose any issue. (limited number of content, max 100)

    Video List Adapter

    import java.util.ArrayList;
    import java.util.Formatter;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Locale;
    
    import android.content.ContentResolver;
    import android.content.Context;
    import android.content.res.Resources;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.LinearLayout.LayoutParams;
    
    public class VideoListAdapter extends BaseAdapter {
        private Context mContext = null;
        private HashMap<String, VideoListItem> mHashedItems = new HashMap<String, VideoListItem>(); 
        private static final String TAG = "VideoListAdapter";
    
        public static final int VIDEO_CONTENT_ID       = 0;
        public static final int VIDEO_CONTENT_TITLE    = 1;
        public static final int VIDEO_CONTENT_DURATION = 2;
        public static final int VIDEO_CONTENT_RESOLUTION = 3;
        public static final int VIDEO_CONTENT_MIME = 4;
    
        private Cursor mCursorForVideoList = null;
        private ContentResolver mContentResolver = null;
        private int mListCount = 0;
    
        VideoListAdapter(Context context, ContentResolver cr) {
            mContext         = context;
            mContentResolver = cr;
            Log.i(TAG, "In the Constructor");
    
            mCursorForVideoList = 
                mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
                                      new String[] { MediaStore.MediaColumns._ID, 
                                                     MediaStore.MediaColumns.TITLE, 
                                                     MediaStore.Video.VideoColumns.DURATION,
                                                     MediaStore.Video.VideoColumns.RESOLUTION
                                                   }, 
                                      null, 
                                      null,  
                                      null);
            mListCount = mCursorForVideoList.getCount();
        }
    
        @Override
        public int getCount() {
            return mListCount;
        }
    
        @Override
        public Object getItem(int arg0) {
            return getVideoListItem(arg0);
        }
    
        @Override
        public long getItemId(int position) {
            //Log.i(TAG, "position : " + position);
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //Log.i(TAG, "GetView :: Position : " + position);
            return getVideoListItem(position);
        }
    
        private VideoListItem getVideoListItem(int position)
        {
            //Log.i(TAG, "getVideoListItem :: Position : " + position);
            String key = Integer.toString(position);
            VideoListItem item = mHashedItems.get(key);
            if(item == null)
            {
                //Log.i(TAG, "New getVideoListItem :: Position : " + position);
                mCursorForVideoList.moveToPosition(position);
                mHashedItems.put(key, new VideoListItem(mContext, mContentResolver, mCursorForVideoList));
            }
            return mHashedItems.get(key);
        }
    
    };
    

    Video List View

    import java.util.Formatter;
    import java.util.Locale;
    
    import android.content.ContentResolver;
    import android.content.Context;
    import android.content.res.Resources;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.graphics.Typeface;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TableLayout;
    import android.widget.TextView;
    import android.widget.LinearLayout.LayoutParams;
    
    class VideoListItem extends LinearLayout
    {
        private static final String TAG = "VideoListAdapter";
    
        private ImageView mThumbnail = null;
        private TextView mDuration   = null;
        private TextView mTitle      = null;
        private TextView mResolution = null;
    
        private LayoutInflater mLayoutFactory = null;
    
        private long mContentId = 0;
    
        public VideoListItem(Context context, ContentResolver cr, Cursor cursor) {
            super(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            params.setMargins(10, 10, 10, 10);
    
            mLayoutFactory = LayoutInflater.from(context);
            View thisView = mLayoutFactory.inflate(R.layout.videolistitem, null);
            addView(thisView);
    
            mThumbnail  = (ImageView) findViewById(R.id.thumbnail); 
            mDuration   = (TextView)  findViewById(R.id.DDuration);
            mTitle      = (TextView)  findViewById(R.id.DTitle);
            mResolution = (TextView)  findViewById(R.id.DResolution);
    
            mThumbnail.setLayoutParams(new LinearLayout.LayoutParams(144, 144));
    
            Resources r = this.getResources();
            Bitmap bMap = MediaStore.Video.Thumbnails.getThumbnail(cr, cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID), MediaStore.Video.Thumbnails.MINI_KIND, null);
            if(bMap != null)
            {
                mThumbnail.setImageBitmap(Bitmap.createScaledBitmap(bMap, 128, 128, true)); 
            }
            else
            {
                mThumbnail.setImageDrawable(r.getDrawable(R.drawable.error));
            }
            mThumbnail.setPadding(16, 16, 16, 16);
            mTitle.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_TITLE));
            mTitle.setSingleLine();
            mTitle.setTextColor(Color.GREEN);
    
            mResolution.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_RESOLUTION));
            mResolution.setSingleLine();
            mResolution.setTextColor(Color.RED);
    
            mDuration.setText(stringForTime(cursor.getInt(VideoListAdapter.VIDEO_CONTENT_DURATION)));
            mDuration.setSingleLine();
            mDuration.setTextColor(Color.CYAN);
    
            mContentId = cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID);
        }
    
        public long getContentId()
        {
            return mContentId;
        }
    
        private StringBuilder mFormatBuilder = null;
        private Formatter mFormatter = null;
    
        private String stringForTime(int timeMs) {
            int totalSeconds = timeMs / 1000;
    
            mFormatBuilder = new StringBuilder();
            mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
    
            int seconds = totalSeconds % 60;
            int minutes = (totalSeconds / 60) % 60;
            int hours   = totalSeconds / 3600;
    
            mFormatBuilder.setLength(0);
            if (hours > 0) {
                return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
            } else {
                return mFormatter.format("%02d:%02d", minutes, seconds).toString();
            }
        }
    
    };
    

    Shash

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

Sidebar

Related Questions

I have a listview with several items that are created dynamically, each has two
I have a ListView that has two columns, one for a zone name and
I have a Listview that has three columns. The last columns holds an image
Ok I have a ListView that has 2 GridViewColumns one displaying a number and
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton
I have a ListView that has a custom ArrayAdapter with a custom XML row.
It seems that android has enforced that a listview must have the name android:id=@android:id/list,
i have a TabLayout that has a ListView to show as intent. I would
I have a list view that displays a collection of items, each item has
I have ListView that uses a GridView to display several columns of data. Two

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.