Tried to fix lazy loading of images. Partly succeded, but mostly failed.

This commit is contained in:
Robert Kolner committed 2012-01-18 11:24:34 +01:00
1 parent da9955cd67
commit e1d97a7a7f
2 files changed
+19 -15

No files matched your search

@@ -5,24 +5,25 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ImageLoader extends AsyncTask<String, Void, Drawable> {
public final static Map<String, Drawable> cache = new HashMap<String, Drawable>();
private final ImageView view;
private final static ConcurrentMap<String, Drawable> cache = new ConcurrentHashMap<String, Drawable>();
private final ViewGroup parent;
private final int position;
private final int imageId;
public ImageLoader(ImageView... views) {
if (views.length > 1)
throw new InvalidParameterException("Too many arguments to the function!");
this.view = views[0];
public ImageLoader(ViewGroup parent, int position, int imageId) {
this.parent = parent;
this.position = position;
this.imageId = imageId;
}
@Override
@@ -50,8 +51,13 @@ public class ImageLoader extends AsyncTask<String, Void, Drawable> {
protected void onPostExecute(Drawable result) {
super.onPostExecute(result);
if (result != null && view != null) {
view.setImageDrawable(result);
if (result != null && parent != null) {
ViewGroup row = (ViewGroup)parent.getChildAt(position);
if (row != null) {
ImageView image = (ImageView)row.findViewById(imageId);
image.setImageDrawable(result);
}
}
}
}
@@ -18,7 +18,6 @@ import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
@@ -108,8 +107,7 @@ public class EventListActivity extends BaseDnsActivity {
final Event e = getItem(position);
/* Set images: */
ImageView imageView = (ImageView) row.findViewById(R.id.event_list_row_image);
ImageLoader loader = new ImageLoader(imageView);
ImageLoader loader = new ImageLoader(parent, position, R.id.event_list_row_image);
loader.execute(e.getImageUri(imageSize).toString());