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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:31:33+00:00 2026-06-15T11:31:33+00:00

I have a custom list view with an image. I need to set the

  • 0

I have a custom list view with an image. I need to set the source of the image via a url but I don’t want the process to block the UI so I’m using an Async task. However the images are being set on only one image on the list and it shows up like a slideshow.

Is there a better way to go about this?

Here is my code:

Custom Adapter

class MyArrayAdapter extends ArrayAdapter<String> {

    private final Context context;
    private int i = 0;
    Contact[] myContact;
    String imageURL;

    public MyArrayAdapter(Context context, Contact[] contact) {
        super(context, R.layout.custom_list);
        this.context = context;
        this.myContact = contact;
        int s = myContact.length;
        int k = contact.length;
    }

    @Override
       public int getCount() {
           return myContact.length;
       }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        rowView = inflater.inflate(R.layout.custom_list, parent, false);
        TextView contact_first_name = (TextView) rowView
                .findViewById(R.id.first_name);
        TextView contact_last_name = (TextView) rowView
                .findViewById(R.id.last_name);
        imageView = (ImageView) rowView.findViewById(R.id.photo);
        imageURL = myContact[i].getImageUrl();          
        new LoadImageTask().execute(imageURL);

        contact_first_name.setText(myContact[i].getFirstName());
        contact_last_name.setText(myContact[i].getLastName());

        i++;

        return rowView;
    }       
}

Async Task

public class LoadImageTask extends AsyncTask<String, Integer, String> {

    Bitmap bitmap;

    @Override
    protected String doInBackground(String... imageURL) {
        // TODO Auto-generated method stub
        try {
              bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL[0]).getContent());

            } catch (MalformedURLException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }
        return null;
    }

    protected void onPostExecute(String line) {

        try {
            imageView.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 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-15T11:31:34+00:00Added an answer on June 15, 2026 at 11:31 am

    Pass the imageview object when executing.
    Use setTag to pass the url. The following will fix your problem, but I advised you to a good look at this tutorial. It will prevent you having out of memory and performances problems.

      @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            rowView = inflater.inflate(R.layout.custom_list, parent, false);
            TextView contact_first_name = (TextView) rowView
                    .findViewById(R.id.first_name);
            TextView contact_last_name = (TextView) rowView
                    .findViewById(R.id.last_name);
            imageView = (ImageView) rowView.findViewById(R.id.photo);
            imageURL = myContact[i].getImageUrl();
            imageView.setTag(imageURL);    // add this      
            new LoadImageTask().execute(imageView); // change here
    
            contact_first_name.setText(myContact[i].getFirstName());
            contact_last_name.setText(myContact[i].getLastName());
    
            i++;
    
            return rowView;
        }   
    

    In the async task:

    public class LoadImageTask extends AsyncTask<ImageView, Integer, String> { // change here
    
        Bitmap bitmap;
       ImageView image = null; // add this
    
        @Override
        protected String doInBackground(String... imageURL) {
            // TODO Auto-generated method stub
        this.image = imageURL[0]; // add this
            String url = (String) image.getTag(); // add this
    
            try {
                  bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent()); // change here
    
                } catch (MalformedURLException e) {
                  e.printStackTrace();
                } catch (IOException e) {
                  e.printStackTrace();
                }
            return null;
        }
    
        protected void onPostExecute(String line) {
    
            try {
                image.setImageBitmap(bitmap); // change here
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    This will fix your problem.

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

Sidebar

Related Questions

I have set onItemClicklistener on my custom list view but onItemClicklistener not working I
I have implemented a Custom List View which displays text along with image. The
I'm working on application where I have a custom made List View, with image
I have a custom list view with image view and text view.I am loading
I have a custom list view that is getting data from server and changing
I have created a custom list view, each row has it's own custom selector,
i have a list view with a BaseAdapter for custom view.. i have problem
I have a custom listview, i successfully parse the data into a list but
I have created a custom listview using adapter. Each row of the list view
For example if i have to create custom list view about type of food,

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.