How to update an ImageView in a custom ListView using ASyncTask?
<p>I know there's already tons of similar questions, but I checked over twenty answers and none of them solves my problem.</p>

<p>First, I have an adapter extending <code>SimpleCursorAdapter</code>. I use custom view in it, including an <code>ImageView</code>. The layout is:</p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="@dimen/listitem_height" android:gravity="center_vertical"&gt;
&lt;ImageView android:id="@+id/row_image" android:layout_width="@dimen/listitem_height"
    android:layout_height="fill_parent" android:paddingRight="@dimen/spaceLarge"
    android:layout_centerVertical="true" android:layout_marginLeft="@dimen/spaceMedium"
    android:src="@drawable/unknown_album" /&gt;
&lt;LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical"
    android:gravity="center_vertical|left"&gt;
    &lt;TextView android:id="@+id/row_text" android:layout_width="fill_parent"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:gravity="center_vertical|left" android:layout_height="wrap_content" /&gt;
    &lt;TextView android:id="@+id/title" android:layout_width="fill_parent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:gravity="center_vertical|left" android:layout_height="wrap_content"
        android:textColor="#95D0B4" /&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p></p>

<p>Then, the <code>ImageView</code> needs some time to load image, so if I do it in main thread, the scrolling will become horrible. Thus I want to use an <code>AsyncTask</code> to load images, but now I face the problem of <code>ListView</code>, which reuses views when scrolling.</p>

<p>That is, code below does not work:</p>

<pre><code>class ImageSetter extends AsyncTask&lt;String, Void, Bitmap&gt; {

    private ImageView iv;

    ImageSetter(ImageView iv) {
        this.iv = iv;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        String path = (String) params[0];
        try {
            return BitmapFactory.decodeFile(path);
        } catch (OutOfMemoryError e) {
            Log.w(Utils.TAG, "", e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        iv.setImageBitmap(result);
        super.onPostExecute(result);
    }

}
</code></pre>

<p>which I call <code>execute()</code> in <code>bindView()</code> in the adapter. The <code>ImageView</code> changes while scrolling, and the result Bitmap is set into wrong position.</p>

<p>Does anyone how to resolve this problem? Thank you very much!</p>

<p>P.S. I also meet serious <code>OutOfMemory</code> problem when scrolling the list fast. I quite have no idea about how to avoid the bitmap memory leak problem, since I can't recycle them as long as they are in the list.</p>
