Skip to content

Lots of Lists: Part 2, List with custom objects and adapter

Note: This is part two of a tutorial series. Each part can be read independently, but you can find the other tutorials at the bottom of this article

In Part 1 of Lots of Lists, we examined how easy it is to create a screen that shows a list. However, real-world lists are rarely the entire layout, and even more rarely a simple lists of strings.

In this tutorial we’ll look at:

  1. What is an adapter? New Project! (Page 1)
  2. Custom Object and List Item Layout (Page 2)
  3. Custom Adapter (Page 3)
  4. All changes together (Page 4)

Here is a picture of our goal:

Lots of Lists 2: News Entries list

Lots of Lists 2: News Entries list

Download this project now: GitZIP ] or continue reading to learn about ListViews with custom adapters!

What is an Adapter?

An adapter is how Android translates data from some data source onto a view. Typically adapters are used on ListViews and GridViews, and Android provides some very helpful adapters out of the box.

The adapter goes hand-in-hand with a corresponding view (which in our case is a ListView.) Together they bring cleaner code, optimized performance, and reusability to your project.

Cleaner separation of concerns

When you add an object to an adapter, the action also adds the representative view for that object to any lists bound to the adapter. Your code that is adding data into the list through the adapter doesn’t have to know how the list is built for each item. Likewise, the code that knows how to build views for each item in your list doesn’t have to know where the data is coming from.

This makes a somewhat clean separation of concerns that normally overlap in one spot. Those of you who have written many lines of code to populate UI elements with varied data can understand how tangled the project becomes when the code retrieving data is also the code rendering it.

Optimizations: View Recycling

Another great benefit of using a ListView/adapter is the immense performance gains it can bring. If you’ve ever built a ScrollView with a list of child views, you’ll notice that performance becomes slow quickly. The ListView/adapter pair solves this with a traditional technique: view recycling.

View recycling is a technique in which view objects are re-used once their current locations are no longer visible. This has a great performance advantage– as your list of items grows, the total number of views remains the same, which corresponds to the distinct number of views drawn in a single screenful. Even better yet, the process is handled behind the scenes, so your adapter should only be aware that views are reused, not necessarily how they are reused.

Code Reusability

Finally the code you write for an adapter, and the separation it forces in the data, view, and adaptation concerns, will result in your code being much more reusable. Where you may have previously written all of the code directly into some Activity for displaying a list of complex objects, now you have a separate class that you can reuse for other lists. In fact, you can even vary the layout the adapter uses without changing anything about the adapter itself.

Now that we’ve covered a tiny portion of adapters in theory, let’s move on to practice. First we need a new project.

New Project

Create a new Android Project (2.1+) in Eclipse (If you’re not using Eclipse: Getting Setup with Android.)

You can name package and Activity whatever you would like. You should be able to immediately run this project on a device or emulator. Here’s what we have for our Activity and Layout:

Starting activity: MainActivity:

Starting layout: res/layout/main.xml

Running this project gives us a pretty simple screen:

Lots of lists 2: Hello World

Lots of lists 2: Hello World

We have a project that is up and running– now we need to actually add a ListView to our layout.

ListView in LinearLayout

A ListView is pretty self-explanatory– it’s a view that groups child views into a list. While it’s a little complex behind the scenes (as we’ll see with the adapter below), the actual layout we need for the view is very simple.

Here is the new main.xml with our ListView.

Pretty simple right? That’s all we’ll need for the final product. In real-world scenarios we would likely see a much more complex layout in main.xml, however, we have achieved our first object: ListView within a standard layout.

The next objective covers the majority of this tutorial, which is binding custom objects using a custom adapter.

{ 5 } Comments