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.

We recommend you place all the code that will do the real work of binding inside your CrimeHolder. First, add a property to stash the Crime being bound. While you are at it, make the existing text view properties private. Add a bind(Crime) function to CrimeHolder. In this new function, cache the crime being bound into a property and set the text values on titleTextView and dateTextView ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.