'OnClickListener on button inside a RecyclerView /ViewHolder

I'm trying to delete items from a list, which is shown as a RecyclerView. Furthermore I would like to choose a item and open an new activity. (Data stored as Shared Preferences) How do I get the itemname? here is, how my list look like with the delete and edit buttons

MainActivity Class - GroupActivity

class GroupActivity : AppCompatActivity() {

private lateinit var viewModel: GroupViewModel
private lateinit var binding: GroupActivityBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    viewModel = ViewModelProvider(this,
        GroupViewModelFactory(PreferenceManager.getDefaultSharedPreferences(this))
    )[GroupViewModel::class.java]

    binding = GroupActivityBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)

    if (savedInstanceState == null) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.container, GroupFragment.newInstance())
            .commitNow()
    }

    val username: String? = intent.getStringExtra("StringName")
    supportActionBar?.title = getString(R.string.txt_welcome_group,username)

    binding.fabAddGroup.setOnClickListener{showCreateListDialog()}
    
}

ViewModel Class - GroupViewModel

class GroupViewModel (private val sharedPreferences: SharedPreferences) : ViewModel() {
lateinit var onListAdded: (() -> Unit)
lateinit var onListDeleted: (() -> Unit)

val lists: MutableList<GroupList> by lazy {
    retrieveLists()
}

private fun retrieveLists(): MutableList<GroupList> {
    val sharedPreferencesContents = sharedPreferences.all
    val allGroupsList = ArrayList<GroupList>()         //Array, in welches die einzelnen Elemente des SHP geschrieben werden

    for (group in sharedPreferencesContents) {
        val itemsHashSet = ArrayList(group.value as HashSet<String>)
        val list = GroupList(group.key, itemsHashSet)
        allGroupsList.add(list)
    }
    return allGroupsList
}

fun saveList(group: GroupList) {
    sharedPreferences
        .edit()
        .putStringSet(group.nameKey, group.groups.toHashSet())
        .apply()
    lists.add(group)
    onListAdded.invoke()
}

fun deleteList(group: GroupList){
    sharedPreferences
        .edit()
        .remove(group.nameKey)
        .apply()
    lists.remove(group)
    onListDeleted.invoke()
}

ViewHolder Class - ListSelectionViewHolder

class ListSelectionViewHolder(val binding: ListSelectionViewHolderBinding): RecyclerView.ViewHolder(binding.root) {

}

Adapter class - ListSelectionRecyclerViewAdapter

class ListSelectionRecyclerViewAdapter(private val lists: MutableList<GroupList>): RecyclerView.Adapter<ListSelectionViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListSelectionViewHolder {

    val binding = ListSelectionViewHolderBinding.inflate(LayoutInflater.from(parent.context), parent, false)
    return ListSelectionViewHolder(binding)
}

override fun onBindViewHolder(holder: ListSelectionViewHolder, position: Int) {
    holder.binding.txtGroupNo.text = (position + 1).toString()
    holder.binding.txtGroupName.text = lists[position].nameKey
}

override fun getItemCount(): Int {
    return lists.size
}


fun listsUpdated() {
    notifyItemInserted(lists.size - 1)
}

}

Can anybody help me?

Thanks a lot! :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source