Skip to content Skip to sidebar Skip to footer

How To Display An Image From The Internet In Simpleadapter

I have SimpleAdapter in which I want to display an image from the Internet. final String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_IMAGE,ATTRIBUTE_NAME_DESCRIPTION }; final in

Solution 1:

Hi you can use an Universal Image loader for this purpose, what you can do is make a custom list , before show a progress icon or dialog and later replace that with an image loaaded from the internet please visit

https://github.com/nostra13/Android-Universal-Image-Loader

Solution 2:

It is much better to use a custom adapter as @Raghunandan suggested. You have much more control and it's not that hard.

First, create a class that extends ArrayAdapter and pass the type of object you want to handle:

publicclassMyCustomAdapterextendsArrayAdapter<MyObjectModel> {

Your adapter must contain a Collection (Lists are fine) of your MyModelObject objects. You can pass this list in the constructor of the adapter and keep a reference in a variable. Each object should have a property with the URL of an image.

Second, create a simple layout for each item in your list. Your layout must contain an ImageView somewhere:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"... ><ImageViewandroid:id="@+id/myImage"android:layout_width="wrap_content"android:layout_height="wrap_content"... />

Third, back to your adapter, you must implement the getView() method. This method returns a view, based on your custom layout, that contains the info from your MyObjectModel object at the given position in the list.

  • Inflate your layout : view = inflater.inflate(R.layout.custom_list_item, null);
  • Get your ImageView component : imageView = (ImageView)view.findViewById(R.id.myImage);
  • Get the object from the list : item = list.get(position);
  • Download the image in an asynctask or use a 3rd party tool such as Universal Image Loader, Picasso etc. Personally I use UrlImageViewHelper : UrlImageViewHelper.setUrlDrawable(imageView, item.imageURL, ...);

IMO it's the only way to have a nice and custom list because default Android options don't allow much customization.

Have a look at http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ for some tips to improve your custom adapter.

Post a Comment for "How To Display An Image From The Internet In Simpleadapter"