Add clickable button to output text dynamically

I wish to add an optional delete button to my row output. The delete button must only delete the row entry I click on with the id passed to a delete function. I can set an onclick event listener on the row, but have not been successfull as it always just deletes the last entry on the last row outputted.

How do I create a button to add to my output string, and set a listener on it dynamically?

My test code is currenly looking like this using a text string as the clickable item. I would like a button

// Append data to text views
txt.text = "Del"
txt.id = i

name.append("$personName\n")
age.append("$personAge\n")
btn.append(txt.text)
btn.append("\n")

btn.setOnClickListener {
  Toast.makeText(this, "Deleting $personName", Toast.LENGTH_SHORT).show()
  db.deleteItem("$personName")
  listItems()
}
i++