Skip to content

Commit

Permalink
ViewHolder Pattern in Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
butterflyforever committed Aug 19, 2020
1 parent d10f35f commit 3c58154
Showing 1 changed file with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextView>(R.id.single_run_time)
val datetimeTextView = rowView.findViewById<TextView>(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
}

}
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)
}

}

0 comments on commit 3c58154

Please sign in to comment.