Android Programming: The Big Nerd Ranch Guide, 5th Edition
by Bryan Sills, Brian Gardner, Kristin Marsicano, Chris Stewart
Cleaning Up Binding List Items
Right now, the Adapter binds crime data directly to a crime holder’s text views in Adapter.onBindViewHolder(…). This works fine, but it is better to more cleanly separate concerns between the view holder and the adapter. The adapter should know as little as possible about the inner workings and details of the view holder.
Instead, place all the code that will do the real work of binding inside your CrimeHolder.
Start by adding a bind(Crime) function to CrimeHolder.
In this new function, cache the crime being bound into a property and set the text values on crimeTitle and crimeDate.
While you are at it, make the existing binding property private.
Listing 10.13 Writing a bind(Crime) function (CrimeListAdapter.kt ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access