I am building an app that has a listview.In the listview,I added a button to remove item.I made a function to remove item using sqlite.
The problem is that the button doesn't work when i press it to remove item. and the logcat doesn't show any errors.
button in listview items:
<Button
android:id="@+id/del_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="68dp"
android:onClick="delete_item"
/>
fun to remove item:
fun delete_item(view:View){
//delete record
var del_btn=findViewById<Button>(R.id.del_btn)
del_btn.setOnClickListener {
var check_box_id=del_btn.text
var get_number_only=check_box_id.replace("[^0-9]".toRegex(),"")
//creating the instance of DatabaseHandler class
val databaseHandler: DatabaseHandler = DatabaseHandler(this@ListInvests)
//calling the deleteEmployee method of DatabaseHandler class to delete record
val status = databaseHandler.deleteEmployee(
DeleteModelClass(
Integer.parseInt(get_number_only)
)
)
if (status > -1) {
Toast.makeText(applicationContext, "record deleted", Toast.LENGTH_LONG).show()
}
}
}
The problem is that i didn't add the listener in the Adatper Class of listview. So,i added the listener to the adapter class,and attach the findViewById to rowview
Answer from mukesh solved this issue.
//delete record
var del_btn=rowView.findViewById<Button>(R.id.del_btn)
del_btn.setOnClickListener {
var check_box_id=del_btn.text
var get_number_only=check_box_id.replace("[^0-9]".toRegex(),"")
//creating the instance of DatabaseHandler class
val databaseHandler: DatabaseHandler = DatabaseHandler(context)
//calling the deleteEmployee method of DatabaseHandler class to delete record
val status = databaseHandler.deleteEmployee(
DeleteModelClass(
Integer.parseInt(get_number_only)
)
)
if (status > -1) {
Toast.makeText(context, "record deleted", Toast.LENGTH_LONG).show()
}
}