Created
August 9, 2018 18:13
-
-
Save anandwana001/c76f3f8db8199822eeaf7e98b9b9854c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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