I'm new to Android development and I'm trying to display a list of todo on my RecyclerView but it is throwing an error that says 'lateinit property binding has not been initialized'
I think the error is from here :
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.itemView.apply {
binding.txtTitle.text = todos[position].title
binding.cbTodo.isChecked = todos[position].isChecked
}
Because that's the line of code in my Adapter the logcat is highlighting while I checked for error. Please I need help in solving this error , my app crashes whenever I tried entering this Activity.
my Adapter
class TodoAdapter(var todos : List<Todo>) : RecyclerView.Adapter<TodoAdapter.TodoViewHolder>()
{
inner class TodoViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView)
private lateinit var binding:ItemTodoBinding
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_todo,parent,false)
return TodoViewHolder(view)
}
override fun onBindViewHolder(holder: TodoViewHolder, position: Int) {
holder.itemView.apply {
binding.txtTitle.text = todos[position].title
binding.cbTodo.isChecked = todos[position].isChecked
}
}
override fun getItemCount(): Int {
return todos.size
}
}
My Class
data class Todo (
val title :String,
var isChecked : Boolean
)
My Activity.kt
class NextAftertoolbar : AppCompatActivity() {
binding = ActivityNextAftertoolbarBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding.root)
supportActionBar?.hide()
var todolist = mutableListOf(
Todo("Follow Android devs",false),
Todo(" Learn ",false),
Todo("Build",false),
Todo("Land a role",false),
)
val adapter = TodoAdapter(todolist)
binding.rvTodo.adapter = adapter
binding.rvTodo.layoutManager=LinearLayoutManager(this)
binding.btnTodo.setOnClickListener {
val title = binding.etTodo.text.toString()
val todo = Todo(title,false)
todolist.add(todo)
adapter.notifyItemInserted(todolist.size-1)
}
}
}
CodePudding user response:
As answered earlier, you should inflate and provide binding to your ViewHolder.
Here full working code.
class TodoAdapter(var todos: List<Todo>) :
RecyclerView.Adapter<TodoAdapter.TodoViewHolder>() {
inner class TodoViewHolder(val binding: ItemTodoBinding) :
RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): TodoViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemTodoBinding.inflate(inflater, parent, false)
return TodoViewHolder(binding)
}
override fun onBindViewHolder(
holder: TodoViewHolder,
position: Int
) {
holder.apply {
binding.txtTitle.text = todos[position].title
binding.cbTodo.isChecked = todos[position].isChecked
}
}
override fun getItemCount(): Int {
return todos.size
}
}
CodePudding user response:
Please add following code into your onCreateViewHolder method.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoViewHolder {
binding =
ItemTodoBinding.inflate(LayoutInflater.from(context), parent, false)
return TodoViewHolder(binding)
}