From 3c58154651261f0f90386c9b06fbf9b4d39a7c0d Mon Sep 17 00:00:00 2001 From: Yang Li Date: Wed, 19 Aug 2020 15:24:12 -0400 Subject: [PATCH] ViewHolder Pattern in Adapter --- .../quickstart/canonical/RunHistoryAdapter.kt | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunHistoryAdapter.kt b/android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunHistoryAdapter.kt index 1af3d0ae..e637e17a 100644 --- a/android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunHistoryAdapter.kt +++ b/android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunHistoryAdapter.kt @@ -29,15 +29,28 @@ class RunHistoryAdapter( override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { // Get view for row item - val rowView = convertView ?: inflater.inflate(R.layout.single_run_item, parent, false) - val timeTextView = rowView.findViewById(R.id.single_run_time) - val datetimeTextView = rowView.findViewById(R.id.single_run_datetime) + val rowView : View + val holder : ViewHolder + if (convertView == null) { + rowView = inflater.inflate(R.layout.single_run_item, parent, false) + holder = ViewHolder(rowView) + rowView.tag = holder + } else { + rowView = convertView + holder = rowView.tag as ViewHolder + } val singleRun = getItem(position) as ProfileViewModel.SingleRun - timeTextView.text = singleRun.time - datetimeTextView.text = singleRun.dateTime + holder.timeTextView.text = singleRun.time + holder.datetimeTextView.text = singleRun.dateTime return rowView } -} \ No newline at end of file + private class ViewHolder(row: View) { + val timeTextView: TextView = row.findViewById(R.id.single_run_time) + val datetimeTextView: TextView = row.findViewById(R.id.single_run_datetime) + } + +} +