How to load images in custom listview and showing progress bar while doind this in Android?
<p>I read a lot of threads here and elsewhere about loading images in custom listviews but I got nowhere! So, I thought I'd better ask!</p>

<p>The sittuation is as follows:</p>

<p>I have an activity that has a listview on it with 6 items (lets say Item1, Item2 etc). When the user clicks on an item I launch a new activity with another listview but this time the list view is a custom one with an image and some text.</p>

<p>The first activity's onCreate method is this:</p>

<pre><code>GetImageFromURL eventImageLoader = new GetImageFromURL(FirstActivity.this, eventMultimediaID);
eventImageLoader.execute();

try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

CustomAdapter adapter = new CustomAdapter(this, android.R.layout.simple_expandable_list_item_1, R.id.textView1, eventName, bitmap);
eventList = (ListView) findViewById(R.id.secEventList);
eventList.setAdapter(adapter);
eventList.setOnItemClickListener(eventNameListClickListener);
</code></pre>

<p>I create and start an AsyncTask to load the images into a bitmap List.</p>

<p>eventMultimediaID is a String array and so is eventName.</p>

<p>The GetImageFromURL Task is this:</p>

<pre><code>public class GetImageFromURL extends AsyncTask&lt;Void, Void, List&lt;Bitmap&gt;&gt;
{
private ProgressDialog progressDialog = null;
private Context callingContext = null;
private List&lt;Bitmap&gt; bitmap = new ArrayList&lt;Bitmap&gt;();
private String[] eventMultimediaID = null;

public GetImageFromURL(Context callingContext, String[] eventMultimediaID)
{
    this.callingContext = callingContext;
    this.eventMultimediaID = eventMultimediaID;
}

@Override
protected void onPreExecute()
{
    super.onPreExecute();
    progressDialog = ProgressDialog.show(callingContext, "Loading Events", "Retrieving data from server, please wait...", true);
}

@Override
protected List&lt;Bitmap&gt; doInBackground(Void... params)
{       
    try
    {
        int length = eventMultimediaID.length;
        for(int i = 0; i &lt; length; i++)
        {
            URL imageUrl = new URL("https://www.blahblah/servlet?blahblah=" + eventMultimediaID[i]);
            bitmap.add(i, BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream()));
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return bitmap;
}

@Override
protected void onPostExecute(List&lt;Bitmap&gt; bitmap)
{
    for(int i = 0; i &lt; bitmap.size(); i++)
    {
        ShowEventCategory.bitmap.add(i, bitmap.get(i));
    }

    progressDialog.dismiss();
}
}
</code></pre>

<p>and this is the CustomAdapter:</p>

<pre><code>public class CustomAdapter extends ArrayAdapter&lt;String&gt;
{
private Context currentContext;
private String[] data;
private List&lt;Bitmap&gt; evImage;

public CustomAdapter(Context context, int resource, int textViewResourceId, String[] objects, List&lt;Bitmap&gt; bitmap)
{
    super(context, resource, textViewResourceId, objects);
    this.currentContext = context;
    this.data = objects;
    this.evImage = bitmap;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{       
    LayoutInflater inflater = (LayoutInflater) currentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View eventListItem = inflater.inflate(R.layout.show_event_category_list_item, parent, false);

    ImageView eventListItemImage = (ImageView) eventListItem.findViewById(R.id.eventImage);
    TextView eventListItemText = (TextView) eventListItem.findViewById(R.id.eventText);

    eventListItemText.setText(data[position]);
    eventListItemImage.setImageBitmap(evImage.get(position));

    return eventListItem;
}
}
</code></pre>

<p>What I tried to do was to call the async task from the Activity to load the images and while doing so display a loading dialog. After the loading is complete the dialog is dismissed and the adapter takes the filled Bitmap array.</p>

<p>This doesn't work. After the Task is started the execution goes on (I think thats what SHOULD happen) and the adapter gets an empty array so I get an OutOfBoundsException. That's why I placed Thread.sleep(5000) in the main thread to wait for the load operation to complete. But of course that's not an acceptable solution!</p>

<p>:( Any suggetions?! I've spent all day on that thing!!!!
Thanx a lot in advance!</p>
