Displaying Data in a ListView with a Custom Adapter
Every Android application that shows a list of things needs a way to turn plain Java objects into rows on the screen. Whether you are building a contact picker, a feed of AFL scores or the order history for an ANZ-style bank account, the widget and the adapter behind it are what separate a throwaway prototype from something a real user in Adelaide or Darwin would keep on their home screen.
The quickest route is ArrayAdapter, which inflates one layout and dumps a string into it. That works for demos and short lists of labels. The moment a row carries a name and a phone number, an avatar and a status line, or a different design depending on the type of record, the built-in behaviour stops being enough and you need to take control of the view creation yourself.
This walkthrough builds a custom BaseAdapter for a customer contact list of the kind a small Melbourne business might keep on a tablet near the counter. It then tightens that adapter with a ViewHolder pattern and looks at where RecyclerView changes the picture. The code is Java so it drops straight into an Android Studio project, and the logic translates cleanly if your team works in Kotlin.
A few Australian wrinkles are worth keeping in mind throughout. Dates in Australian apps are usually written DD/MM/YYYY and times on a 24-hour clock, and regional users on metered mobile data will thank you for a list that recycles rows instead of inflating new ones on every fling. The sample data also uses Australian English spelling in its string resources, because Organisation and favourite are what your users will actually see.
Why the default adapter runs out of road
ArrayAdapter<String> makes a strong assumption: every row is a single TextView. When you call setText, it picks the first text view it finds and puts the string there. For a list of categories or a settings screen that is completely adequate, and for beginners it is the fastest way to see something working on a device.
Real records are messier. A customer might need a display name, a mobile number, a suburb such as Warrnambool, and a coloured status chip that says "paid" or "overdue". Those are four separate views, and each one needs its own reference, its own formatting rules and its own behaviour when the data changes.
A custom adapter solves this by handing you the parent view group and the position, and letting you inflate and populate the row exactly how you want. Because you write the binding logic, you also control accessibility labels, currency formatting for Australian dollars, and the way a timestamp reads back as "2:45 pm" rather than a raw ISO string.
Building the adapter class
A custom adapter extends android.widget.BaseAdapter and overrides four methods: getCount, getItem, getItemId and getView. The first three are trivial — they simply expose the size of your collection, the object at a position and a stable identifier for it. The fourth is where the real work happens.
public class ContactAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final List<Contact> items;
public ContactAdapter(Context context, List<Contact> items) {
this.inflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
row = inflater.inflate(R.layout.row_contact, parent, false);
}
Contact contact = (Contact) getItem(position);
TextView name = row.findViewById(R.id.text_name);
TextView number = row.findViewById(R.id.text_number);
name.setText(contact.getName());
number.setText(contact.getPhone());
return row;
}
}
The convertView parameter is the recycling mechanism. When a row scrolls off the screen it is kept in a pool and handed back to you for the next position, so inflating it again wastes memory and CPU. Inflating only when convertView is null is the single most important habit in this whole pattern.
One caution: never call findViewById outside the getView method. Doing it in the constructor for every item turns a smooth list into a stuttering one, and it is a common mistake in code copied from older tutorials written for very early Android releases.
Designing the row layout
The inflated layout is where the design lives. A typical contact row is a horizontal LinearLayout with an ImageView for an avatar and a nested vertical LinearLayout holding two TextView elements, plus an optional status TextView aligned to the parent.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="12dp">
<ImageView
android:id="@+id/image_avatar"
android:layout_width="48dp"
android:layout_height="48dp"
android:contentDescription="@string/contact_avatar_description" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_marginStart="12dp">
<TextView
android:id="@+id/text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
Keep the hierarchy shallow. A deeply nested row layout multiplied by a hundred visible rows is exactly the kind of thing that makes a phone in a regional area with patchy coverage feel like it is struggling. Where you can use a ConstraintLayout instead of four nested LinearLayout elements, do so.
Put every user-visible string in res/values/strings.xml, and remember that Australian English differs from the default locale strings on many devices. If your list shows a booking time, format it with DateFormat using DateFormat.is24HourFormat(context) so it lines up with what the user's phone shows, and write dates as day-first rather than month-first.
Connecting the adapter to your data
The adapter is usually created in onCreate once your data is loaded, then attached to the list with setAdapter. Keeping a reference to the adapter means you can refresh it later without rebuilding the whole screen.
List<Contact> contacts = loadContacts();
ContactAdapter adapter = new ContactAdapter(this, contacts);
ListView listView = findViewById(R.id.list_contacts);
listView.setAdapter(adapter);
Click handling belongs on the list, not on the adapter. Pull the object at the tapped position and act on it:
listView.setOnItemClickListener((parent, view, position, id) -> {
Contact selected = (Contact) adapter.getItem(position);
openContactDetail(selected);
});
When the data changes — a sync finishing at arvo, a scoreboard updating after the Friday night game — call notifyDataSetChanged() to have the list redraw. That method is blunt and rebuilds every visible row, so if your list is long and updates often, filter into a separate ArrayList and hand that to the adapter instead of mutating the original.
For a searchable list, extend Filter and override performFiltering so filtering happens off the main thread. That keeps the list responsive while a user types "North Adelaide" into the search field.
Weighing your options and finishing the build
ListView remains a perfectly capable choice, especially for short, stable lists such as a menu of specials at a fish-and-chip shop in Geelong. It needs no extra dependency, it recycles rows through convertView, and it behaves predictably. The trade-off is that you own the recycling code and you get little help with animations or granular updates.
RecyclerView arrived later and solves those problems. It comes from the AndroidX recyclerview artifact, ships with a ViewHolder pattern built in, animates inserted and removed rows through its ItemAnimator, and pairs with ListAdapter and DiffUtil for precise, efficient updates. Weighing the two side by side makes the choice clearer:
| Consideration | ListView with BaseAdapter | RecyclerView with ListAdapter |
|---|---|---|
| Row recycling | Handled manually through convertView |
Automatic with ViewHolder |
| Extra dependency | None beyond the app's existing AndroidX libraries | Requires androidx.recyclerview |
| Update mechanism | notifyDataSetChanged() redraws everything |
DiffUtil computes granular changes |
| Animation support | Minimal without extra work | Item animations included |
| Best suited to | Short, stable lists | Long lists that change often |
Whatever you pick, the habits below keep the screen quick and the code maintainable.
- Inflate a row only when
convertViewis null, and find views once per row rather than on every scroll event. - Keep the row layout shallow and avoid
findViewByIdcalls in the constructor. - Format dates, times and dollar amounts with locale-aware helpers so
04/07/2026and$18.50read naturally to Australian users. - Provide an empty-state view and a
contentDescriptionon any image, so the list stays usable with TalkBack enabled. - Test with a large data set, and profile on a mid-range device rather than assuming the emulator tells the whole story.