0

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) {
}
  • 1
    My recommendation is to step back and redesign it so the adapter is not also your list manager. An adapter should only be responsible for displaying a list and reporting when widgets in that list are interacted with. It should not be responsible for modifying the list or the contents of the list, because that gets very messy very quickly. That also prevents you from using ListAdapter, which provides better performance and animations. – Tenfour04 Sep 03 '22 at 17:52
  • Also, if your app supports screen rotation, you probably want to store your list in a ViewModel anyway rather than in an adapter, where it will be destroyed when the screen rotates. – Tenfour04 Sep 03 '22 at 17:54
  • @Tenfour04 But I want to delete the item which user selects. How can I do this from outside the onbindViewHolder function because I will need the position of the item which is selectd. I am beginner in android please tell me if there is any way to do this from outside of the onbindViewHolder function – Divyansh Singh Sep 04 '22 at 13:02
  • https://stackoverflow.com/questions/49969278/recyclerview-item-click-listener-the-right-way This is in Java. You can also Google search for implementing listeners/callbacks in your adapter. The listener interface should pass either the item itself or the index, and the Activity/Fragment can connect that to ViewModel functions to handle it. – Tenfour04 Sep 04 '22 at 16:54
  • @Tenfour04 But My problem is that I am not able to call the selectAll function. I want it inside the MainActivity class because I have inflated the menu inside the mainActivity. Is there any way that I could call the function or is there any way I could set the click listener for selectAll menu inside the adapter?. Or I have to inflate the menu button inside the adapter to select the items? – Divyansh Singh Sep 05 '22 at 18:21
  • I'm saying the Adapter should only react to changes in the list. It should not be responsible for doing anything to the list or the items in the list. Keep the list in a ViewModel. Expose functions in the ViewModel for doing things like "select all". The ViewModel function would copy the list and modify it, then publish it back to a LiveData. The Activity is observing the livedata so whenever a new list appears, it passes it to the Adapter. – Tenfour04 Sep 05 '22 at 22:19

0 Answers0