Skip to content

Instantly share code, notes, and snippets.

@anandwana001
Created August 9, 2018 18:13
Show Gist options
  • Save anandwana001/c76f3f8db8199822eeaf7e98b9b9854c to your computer and use it in GitHub Desktop.
Save anandwana001/c76f3f8db8199822eeaf7e98b9b9854c to your computer and use it in GitHub Desktop.
class HabitListAdapter internal constructor(context: Context) : RecyclerView.Adapter<HabitListAdapter.HabitViewHolder>() {
private val mInflater: LayoutInflater
private var habitsList: List<Habit>? = null
init {
mInflater = LayoutInflater.from(context)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HabitViewHolder {
val itemView = mInflater.inflate(R.layout.recyclerview_item, parent, false)
return HabitViewHolder(itemView)
}
override fun onBindViewHolder(holder: HabitViewHolder, position: Int) {
if (habitsList != null) {
val (habit) = habitsList!![position]
holder.habitItemView.text = habit
} else {
holder.habitItemView.text = "No Word"
}
}
override fun getItemCount(): Int {
return if (habitsList != null)
habitsList!!.size
else
0
}
internal fun setWords(habitsList: List<Habit>) {
this.habitsList = habitsList
notifyDataSetChanged()
}
inner class HabitViewHolder(itemView: View) : ViewHolder(itemView) {
val habitItemView: TextView
init {
habitItemView = itemView.findViewById(R.id.textView)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment