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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:58:23+00:00 2026-06-09T09:58:23+00:00

I have this strange problem about loading images using AsyncTask in ListView.In my ListView,

  • 0

I have this strange problem about loading images using AsyncTask in ListView.In my ListView, every row contains an ImageView and a TextView. I followed this link for it:

http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html

The images are being downloaded successfully from the URL and are populated on their respected rows. But when I scroll the ListView or click on any list item, the images just exchange their rows. Though the text in the TextViews remain on the same rows. I don’t understand why its happening. I have googled a lot about it but can’t find a perfect reason for it. Please help.

Here is my adapter class:

     private class ListAdapter extends BaseAdapter{

    private ArrayList<HashMap<String, Object>> allFriends; 
    private LayoutInflater mInflater;

    public ListAdapter(ArrayList<HashMap<String, Object>> allFriends, Context context){
        this.allFriends = allFriends;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return allFriends.size();
    }

    @Override
    public Object getItem(int position) {
        return allFriends.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        FriendsViewHolder holder;

        if (convertView == null||!(convertView instanceof TextView)||!(convertView instanceof ImageView)) {
             convertView = mInflater.inflate(R.layout.friend_list_view, null);

             holder = new FriendsViewHolder();
             holder.friendName = (TextView) convertView.findViewById(R.id.friendName);
             holder.friendImage = (ImageView) convertView.findViewById(R.id.friendImage);                

             convertView.setTag(holder);
         }else {
             holder = (FriendsViewHolder) convertView.getTag(); 
         }

        holder.friendName.setText((String) allFriends.get(position).get(FriendsActivity.FRIENDS_NAME_KEY));

        String otherId=(String) allFriends.get(position).get(FriendsActivity.IDKEY);
        String isImage=(String) allFriends.get(position).get(FriendsActivity.IS_IMAGE_KEY);

            if(isImage.equalsIgnoreCase("true")){
                download(otherId, holder.friendImage);
            }

        return convertView;
    }
}//End of list adapter

and the download method is:

   public void download(String otherId, ImageView imageView) {
     BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
     task.execute(otherId);
 }

Here BitmapDownloaderTask is the async task for downloading the images:

    class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        private String otherId;
        private final WeakReference<ImageView> imageViewReference;

        public BitmapDownloaderTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) {
             // params comes from the execute() call: params[0] is the url.

             return getOtherUserImage(params[0]);
        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if(bitmap!=null){
                if (imageViewReference != null) {
                    ImageView imageView = imageViewReference.get(); 
                    if(imageView != null ){
                        imageView.setImageBitmap(bitmap);
                    } 
                }
            }
        }
    }

And getOtherUserImage method is:

    public Bitmap getOtherUserImage(String otherUserId){

    // code to download the image goes here. It returns bitmap "bmImg".

    if(bmImg==null){
        return null;
    }else {
        bmImg = Bitmap.createScaledBitmap(bmImg,imageWidth, imageHeight, true);

        /*availableFriends.get(position).put(BITMAP_KEY, bmImg);
        runOnUiThread(new Runnable() {
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });*/
        return bmImg;
    }
}
  • 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-09T09:58:24+00:00Added an answer on June 9, 2026 at 9:58 am

    This Android Developer’s Blog post describes how to correctly load images into a ListView asynchronously and provides a great sample app that you should be able to quickly and easily modify (I did) to fit your needs.

    Indeed, for memory efficiency reasons, ListView recycles the views
    that are displayed when the user scrolls. If one flings the list, a
    given ImageView object will be used many times. Each time it is
    displayed the ImageView correctly triggers an image download task,
    which will eventually change its image. So where is the problem? As
    with most parallel applications, the key issue is in the ordering. In
    our case, there’s no guarantee that the download tasks will finish in
    the order in which they were started. The result is that the image
    finally displayed in the list may come from a previous item, which
    simply happened to have taken longer to download. This is not an issue
    if the images you download are bound once and for all to given
    ImageViews.

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

Sidebar

Related Questions

I have a strange problem. I do this query in a node.js server using
I'm having a strange problem - I have about 14.5 million bitmap images, that
I have this strange problem. I am retrieving twitters and it works on the
i have this strange problem with the PHP function CTYPE_ALNUM if i do: PHP:
Starting from Android 3.2 I have this strange problem. It's very easy to reproduce:
I have a strange problem. I have this method, which should generate a Date
I am facing a strange problem and have no idea how to fix this
I have a problem with a strange crash on iPad 5.0. This crash only
I have a strange problem that I don't understand about DataPager. After changing the
I have this really strange problem where my entity framework query isn't enumerating correctly.

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.