Repository navigation
Expand file tree
/
Copy pathTaskAdapter.kt
More file actions
50 lines (41 loc) · 1.72 KB
/
Copy pathTaskAdapter.kt
File metadata and controls
50 lines (41 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.todolist
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class TaskAdapter(
private val tasks: MutableList<Task>,
private val onDelete: (Int) -> Unit
) : RecyclerView.Adapter<TaskAdapter.TaskViewHolder>() {
class TaskViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val taskTitle: TextView = itemView.findViewById(R.id.task_title)
val taskCheckbox: CheckBox = itemView.findViewById(R.id.task_checkbox)
val deleteButton: Button = itemView.findViewById(R.id.buttonDelete)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.task_item, parent, false)
return TaskViewHolder(view)
}
override fun getItemCount(): Int = tasks.size
override fun onBindViewHolder(holder: TaskViewHolder, position: Int) {
val task = tasks[position]
holder.taskTitle.text = task.title
holder.taskCheckbox.isChecked = task.isDone
// Update UI for completed task
holder.taskTitle.paintFlags = if (task.isDone)
holder.taskTitle.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
else
holder.taskTitle.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
holder.taskCheckbox.setOnCheckedChangeListener { _, isChecked ->
task.isDone = isChecked
notifyItemChanged(position)
}
holder.deleteButton.setOnClickListener {
onDelete(position)
}
}
}