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++
                

I would need to see more of your code, but I’m assuming this is in some kind of loop. In which case, the problem is txt.id = i. Since you increment i every iteration, but are modifying the same txt object, the end of the loop is going to have txt.id being equal to the last value of i.

where I left it last:

                do {val personName =cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.NAME_COL))val personAge = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.AGE_COL))val itemId = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.ID_COL))val txt = TextView(this)
// Append data to text views
                txt.text = "Del"
                txt.id = i

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

                btn.append(txt.text)
                btn.append("\n")

                txt.setOnClickListener {
                    Toast.makeText(this, "Deleting $personName", Toast.LENGTH_SHORT).show()
                    db.deleteItem(name = "$itemId")
                    listItems()
                }

                // creating the button
                /*
                val layout = findViewById(R.id.FirstFragment) as LinearLayout
                val button = Button(this)
                // setting layout_width and layout_height using layout parameters
                button.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                button.text = "Delete"
                button.setOnClickListener { Toast.makeText(this, "Deleteting ...", Toast.LENGTH_LONG).show() }


                // add Button to LinearLayout
                
                // addView?
                btn.append(button)
                */

                i++
            } while (cursor.moveToNext())

I’m not sure, honestly. I don’t know much about Compose, and with that limited code selection, I can’t really figure it out. I’d suggest providing a link to your entire project.

As a newby, here is the link to the whole code. My goal was to adapt this code to add a button next to each line to delete that line.

But it seems I will have to go about this using a totally different approach as it does not seem possible using this type of output view. looking at learning different views and ways in how this is accomplished in kotlin android apps.

Ok, I see what you’re trying to do now. Thanks for the link, that gives me more context.

You still haven’t posted all of your code… unless I’ve missed something, I’m not seeing where btn is declared. My guess is that the problem is that you’re re-using btn, appending text to it, and adding the on-click listeners to the text instead of the button. The button might be inheriting the on-click listener of the last bit of text you add to the button, and when you click on the button, it fires the button’s on-click listener instead of the specific bit of text’s on-click listener. (Or you’re not accurately clicking on the text and are instead clicking on the entire button) I would say create 1 button per row, instead of re-using the same button.

This dont look like compose. Unless you have a hardcoded list, like for a landing page, you might want to use a listview or a recyclerview or go compose and lazycolumn. If you have dynamic data coming in from a db or a remote api. Then you’re gonna contemplate the layout of an entry/item and it can be a row element and it can have your text and a button.