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:
- ArrayAdapter<T> (for displaying an array of objects, using toString() to display them)
- SimpleAdapter (for displaying a list of Maps)
- SimpleCursorAdapter (for displaying rows fetched from a database table using a Cursor)
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:- public class ImageAndText {
- this.imageUrl = imageUrl;
- this.text = text;
- }
- return imageUrl;
- }
- return text;
- }
- }
Now, let's create an implementation of a ListAdapter that is able to display a list of these ImageAndTexts.
- public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
- public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
- super(activity, 0, imageAndTexts);
- }
- @Override
- Activity activity = (Activity) getContext();
- LayoutInflater inflater = activity.getLayoutInflater();
- // Inflate the views from XML
- ImageAndText imageAndText = getItem(position);
- // Load the image and set it on the ImageView
- ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
- imageView.setImageDrawable(loadImageFromUrl(imageAndText.getImageUrl()));
- // Set the text on the TextView
- TextView textView = (TextView) rowView.findViewById(R.id.text);
- textView.setText(imageAndText.getText());
- return rowView;
- }
- InputStream inputStream;
- try {
- }
- return Drawable.createFromStream(inputStream, "src");
- }
- }
The views are inflated from an XML file called "image_and_text_row.xml":
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <ImageView android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/default_image"/>
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </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