0

I got a problem. I got some code which I'm not going to put all here because it's to big. But I got a listview. And i do it like this:

lv = (ListView) findViewById(R.id.list);

(Here is normally some asynctask code and stuff)

// list adapter
ListAdapter adapter = new SimpleAdapter(MainActivity.this, placesListItems,
R.layout.list_item, new String[] { KEY_REFERENCE, KEY_NAME, KEY_VICINITY, KEY_DISTANCE,
     KEY_LOCATION}, new int[] { R.id.reference, R.id.name, R.id.vicinity, R.id.radius, R.id.location});

// Adding data into listview

lv.setAdapter(adapter);

Now the thing is I got an url and the only thing on the site is an image. It's from Google Place Photos. Now I want an image in my listview. So in every item there is another image.

I can do it with an imageview or with a webview.

So let's sey the url is stored in this:

KEY_URL;

And the imageview or webview is this:

R.id.image;

How can I do this?

Thanks in advance!!

Laurenswuyts
  • 2,144
  • 4
  • 21
  • 39

2 Answers2

0

At first,

WebView takes more memory than ImageView. So I recommend you to use ImageView.

At Second,

Check out following URLs.

Lazy load of images in ListView

In this article, The first library, which I have used, is small and customizable library. It uses thread pool and seems to control transfers. The second library, which I haven't used, is larger. But more methods in this library seem to make it easy to control various configurations.

Finally,

I think you should create your own Adapter, extending SimpleAdapter(or CursorAdapter, or…). like following pseudo code.

public class MySimpleAdapter extends SimpleAdapter {
    private ImageLoader mImageLoader;
    private int mResource;
    private LayoutInflater inflater;
    public static class ViewHolder {
        public ImageView img;
        public TextView txt;
    }

    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        mResource = resource;
        mImageLoader = new ImageLoader(context);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.cell_text, null);
            holder      = new ViewHolder();
            holder.txt  = (TextView) convertView.findViewById(R.id.txt);
            holder.img  = (ImageView) convertView.findViewById(R.id.img);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txt.setText("something text");
        mImageLoader.displayImage("url", holder.img);
        return convertView;
    }
}
Community
  • 1
  • 1
Mitsuaki Ishimoto
  • 3,162
  • 2
  • 25
  • 32
0

You Can use [androidQuery]https://code.google.com/p/android-query/ It has a thread pool. It load images in different ways. Has the option to down sample images to save memory. Made by google :)

Tintineg
  • 1
  • 1