Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit 3c58154

Browse files
ViewHolder Pattern in Adapter
1 parent d10f35f commit 3c58154

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunHistoryAdapter.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,28 @@ class RunHistoryAdapter(
2929

3030
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
3131
// Get view for row item
32-
val rowView = convertView ?: inflater.inflate(R.layout.single_run_item, parent, false)
33-
val timeTextView = rowView.findViewById<TextView>(R.id.single_run_time)
34-
val datetimeTextView = rowView.findViewById<TextView>(R.id.single_run_datetime)
32+
val rowView : View
33+
val holder : ViewHolder
34+
if (convertView == null) {
35+
rowView = inflater.inflate(R.layout.single_run_item, parent, false)
36+
holder = ViewHolder(rowView)
37+
rowView.tag = holder
38+
} else {
39+
rowView = convertView
40+
holder = rowView.tag as ViewHolder
41+
}
3542

3643
val singleRun = getItem(position) as ProfileViewModel.SingleRun
37-
timeTextView.text = singleRun.time
38-
datetimeTextView.text = singleRun.dateTime
44+
holder.timeTextView.text = singleRun.time
45+
holder.datetimeTextView.text = singleRun.dateTime
3946

4047
return rowView
4148
}
4249

43-
}
50+
private class ViewHolder(row: View) {
51+
val timeTextView: TextView = row.findViewById(R.id.single_run_time)
52+
val datetimeTextView: TextView = row.findViewById(R.id.single_run_datetime)
53+
}
54+
55+
}
56+

0 commit comments

Comments
 (0)