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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:30:16+00:00 2026-06-11T17:30:16+00:00

i know the title is a bit messy, but here is the problem…. my

  • 0

i know the title is a bit messy, but here is the problem….
my goal is to retrieve title, and draw thumbnails of my youtube channel videos by using thumbnail URL, to the listView…
so far, i have the textView to display video title properly, but the thumbnail just couldnt be draw anyway….. by the way, i have the json / sqlite stuff classes done properly and they can retrieve data properly, so i dont have to worry about that… the only thing that bothers me is thumbnail wont display, the imageView displays as empty space in the app….

here is my code, please give me a hand. thx

this is the on create method of the activity…

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    String[] uiBindFrom = { TutListDatabase.COL_TITLE, TutListDatabase.COL_THUMBNAIL };
    int[] uiBindTo = { R.id.title, R.id.thumbnail };

    getLoaderManager().initLoader(TUTORIAL_LIST_LOADER, null, this);

    adapter = new SimpleCursorAdapter(
            getActivity().getApplicationContext(), R.layout.list_item,
            null, uiBindFrom, uiBindTo,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    adapter.setViewBinder(new MyViewBinder());
    setListAdapter(adapter);
}

and this one is the private class for putting stuff onto listView…

private class MyViewBinder implements SimpleCursorAdapter.ViewBinder{

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch(viewId){
        case R.id.title:
            TextView titleTV = (TextView)view;
            titleTV.setText(cursor.getString(columnIndex));
            break;

                    // it is not displaying any thumbnail in app....
        case R.id.thumbnail:
            ImageView thumb = (ImageView) view;
            thumb.setImageURI(Uri.parse(cursor.getString(columnIndex)));

        break;
        }
        return false;
    }

}

and here is the xml layout file…

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="101dp"
    android:layout_height="101dp"
    android:src="@drawable/icon" />

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dp"
    android:textSize="24dp" />


</LinearLayout>
  • 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-11T17:30:17+00:00Added an answer on June 11, 2026 at 5:30 pm

    I can show you a way that I have used and it worked quite well, first off we need a way to cache the images and the best way I have seen to date, is to use the LruCache as described in the excellent Google IO presentation doing more with less: http://www.youtube.com/watch?v=gbQb1PVjfqM

    Here is my implementation of the method described in that presentation.

    public class BitmapCache extends LruCache<String, Bitmap> { 
    
        public BitmapCache(int sizeInBytes) {
            super(sizeInBytes);
        }   
    
        public BitmapCache(Context context) {
            super(getOptimalCacheSizeInBytes(context));
        }   
    
        public static int getOptimalCacheSizeInBytes(Context context) {
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    
            int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
    
            return memoryClassBytes / 8;
        }
    
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    }
    

    Next we need to load the images asynchronously with an AsyncTask, the following implementation takes care of loading an image into the given ImageView and dealing with the cache:

    public class LoadImageAsyncTask extends AsyncTask<Void, Void, Pair<Bitmap, Exception>> {
        private ImageView mImageView;
        private String mUrl;
        private BitmapCache mCache;
    
        public LoadImageAsyncTask(BitmapCache cache, ImageView imageView, String url) {
            mCache = cache;
            mImageView = imageView;
            mUrl = url;
    
            mImageView.setTag(mUrl);
        }
    
        @Override
        protected void onPreExecute() {
            Bitmap bm = mCache.get(mUrl);
    
            if(bm != null) {
                cancel(false);
    
                mImageView.setImageBitmap(bm);
            }
        }
    
        @Override
        protected Pair<Bitmap, Exception> doInBackground(Void... arg0) {
            if(isCancelled()) {
                return null;
            }
    
            URL url;
            InputStream inStream = null;
            try {
                url = new URL(mUrl);
                URLConnection conn = url.openConnection();
    
                inStream = conn.getInputStream();
    
                Bitmap bitmap = BitmapFactory.decodeStream(inStream);
    
                return new Pair<Bitmap, Exception>(bitmap, null);
    
            } catch (Exception e) {
                return new Pair<Bitmap, Exception>(null, e);
            }
            finally {
                closeSilenty(inStream);
            }
        }
    
        @Override
        protected void onPostExecute(Pair<Bitmap, Exception> result) {
            if(result == null) {
                return;
            }
    
            if(result.first != null && mUrl.equals(mImageView.getTag())) {
                mCache.put(mUrl, result.first);
                mImageView.setImageBitmap(result.first);
            }
        }
    
        public void closeSilenty(Closeable closeable) {
            if(closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                // TODO: Log this
            }
            }
        }
    }
    

    Next you need to create an instance of your BitmapCache in the Activity or Fragment that is hosting the ListView, in onCreate(…) or onActivityCreated(…):

    mBitmapCache = new BitmapCache(this); // or getActivity() if your using a Fragment
    

    Now we need to update the SimpleCursorAdapter that shows the image, I have ommited most of the code as its specific to my project, but the idea is you override setViewImage where the value should be a value that is bound to the cursor, I zap the imageview to null to make sure it does not have an odd image from the cache associated to an item.

        @Override
        public void setViewImage(ImageView iv, String value) {
            final String url = value;
            iv.setImageBitmap(null);
            new LoadImageAsyncTask(mBitmapCache, iv, url).execute();
    
        }
    

    Update

    To make it clear, your adapter should look something like this

        adapter = new SimpleCursorAdapter(
                context, R.layout.list_item,
                null, uiBindFrom, uiBindTo,
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) {
            @Override
            public void setViewImage(ImageView iv, String value) {
                    final String url = value;
                    iv.setImageBitmap(null);
                    new LoadImageAsyncTask(mBitmapCache, iv, url).execute();
            }
        };
    

    Hope that helps!

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

Sidebar

Related Questions

Ok I know the title is a bit confusing but here is my problem:
I know the title of this question is a bit confusing, but here it
I know the title is a little bit strange, but here is what the
So I know I butchered the title a bit but if someone looks here
I know the title is a bit messy, but I don't know how to
I know the title is a bit confusing but here's my situation #!/bin/bash for
i know, the title is a bit crampy, but it's a weird problem. i'm
I know the title is a bit weird, but I was unable to think
I know the title didnt say much, but here I go with a clearer
I know the title is a bit vague. But what I'm trying to achieve

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.