Android ListAdapter example

If you want to use a ListView, you will have to supply it with a ListAdapter to allow it to display any content. A few simple implementations of that adapter are already available in the SDK:
These implementations are perfect for displaying very simple lists. But if your list is just a little more complicated than that, you will need to write your own custom ListAdapter implementation. In most cases it's useful to subclass ArrayAdapter which already takes care of managing a list of objects. Now you only have to tell it how to render each object in the list. Do this by overriding thegetView(int, View, ViewGroup) method of the ArrayAdapter class.
Example of a ListView containing Youtube search results in the form of images and text
The images need to be on-the-fly downloaded from the internet. Let's create a class which represents items in the list:

  1. public class ImageAndText {
  2.     private String imageUrl;
  3.     private String text;
  4.     public ImageAndText(String imageUrl, String text) {
  5.         this.imageUrl = imageUrl;
  6.         this.text = text;
  7.     }
  8.     public String getImageUrl() {
  9.         return imageUrl;
  10.     }
  11.     public String getText() {
  12.         return text;
  13.     }
  14. }

Now, let's create an implementation of a ListAdapter that is able to display a list of these ImageAndTexts.

  1. public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
  2.     public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
  3.         super(activity, 0, imageAndTexts);
  4.     }
  5.     @Override
  6.     public View getView(int position, View convertView, ViewGroup parent) {
  7.         Activity activity = (Activity) getContext();
  8.         LayoutInflater inflater = activity.getLayoutInflater();
  9.         // Inflate the views from XML
  10.         View rowView = inflater.inflate(R.layout.image_and_text_row, null);
  11.         ImageAndText imageAndText = getItem(position);
  12.         // Load the image and set it on the ImageView
  13.         ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
  14.         imageView.setImageDrawable(loadImageFromUrl(imageAndText.getImageUrl()));
  15.         // Set the text on the TextView
  16.         TextView textView = (TextView) rowView.findViewById(R.id.text);
  17.         textView.setText(imageAndText.getText());
  18.         return rowView;
  19.     }
  20.     public static Drawable loadImageFromUrl(String url) {
  21.         InputStream inputStream;
  22.         try {
  23.             inputStream = new URL(url).openStream();
  24.         } catch (IOException e) {
  25.             throw new RuntimeException(e);
  26.         }
  27.         return Drawable.createFromStream(inputStream, "src");
  28.     }
  29. }

The views are inflated from an XML file called "image_and_text_row.xml":

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.              android:orientation="horizontal"
  4.              android:layout_width="fill_parent"
  5.              android:layout_height="wrap_content">
  6.         <ImageView android:id="@+id/image"
  7.                   android:layout_width="wrap_content"
  8.                   android:layout_height="wrap_content"
  9.                   android:src="@drawable/default_image"/>
  10.         <TextView android:id="@+id/text"
  11.                  android:layout_width="wrap_content"
  12.                  android:layout_height="wrap_content"/>
  13. </LinearLayout>

This ListAdapter implementation renders the Image And Texts in the ListView like you would expect. The only thing is that this only works for a very small list which doesn't require scrolling to see all items. If the list of ImageAndTexts gets bigger you will notice that scrolling isn't as smooth as it should be (in fact, it's far off!).

No comments:

Post a Comment