I want to select all the items present in my recycler view using a menu item. I have a function which selects all the items when that menu is pressed. But I am not able to call the function which is selecting all the items from mainactivity so that I can set the click listener to the menu item because it is taking parameter holder and an instance of data class(curTodo).
This is the function in the adapter class of the recycler view
//curTodo is the current item of recycler view
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val curTodo = todoList?.get(position)
//rest of the code is here
}
//This function is selecting all items from recycler view when user clicks on the item
fun selectAllItems(holder: ItemViewHolder, curTodo: Todo) {
if (todoList != null) {
for (item in todoList) {
selectTodo(holder, curTodo)
}
}
}
private fun selectTodo(holder: ItemViewHolder, curTodo: Todo){
curTodo.isSelected = true
holder.ivCheck.visibility = View.VISIBLE
selectedItems.add(curTodo) //selectedItems is a mutable list which stores all the selected items
}
And this is MainAcitivity in which I want to call the function when the selectAll menu item is pressed
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.delete_item, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.menu_delete -> todoAdapter.deleteTodo()
R.id.menu_selectAll -> selectAllItems() // This is the function which i can not call because it requires Parameters holder and Todo
}
return super.onOptionsItemSelected(item)
}
This is data class Todo
data class Todo(val textView: String,
var isSelected: Boolean = false) {
}