简体   繁体   中英

Change recylcerview item's ui on button click from another activity in Kotlin

I have a recyclerview as shown below. When I click on the "Invite to job" button it opens up another activity which has a button. And on that button click I want to disable the selected item's button and change its text to "Invited". :

在此处输入图像描述

This is what I tried. I passed a boolean using the shared preferences when the button from another activity is clicked.

 sharedPref.putBoolean(Constants.CHANGE_INVITE_BUTTON, true)

And then in the recyclerview's adapter I added this inside onBindViewHolder:

 if(sharedPref.getBoolean(Constants.CHANGE_INVITE_BUTTON,true)){
                holder.binding.inviteToJobButtonFreelancerScreenCu.isEnabled = false
                holder.binding.inviteToJobButtonFreelancerScreenCu.text = "Invited"
            }

            else{
                holder.binding.inviteToJobButtonFreelancerScreenCu.setOnClickListener {
                    freelancersFragmentCu.onItemClicked(freelancerFilterList[position])

                }
            }

If the value is true then just change the text and disable the button but if it's not then continue to open the activity with that button. But now when I run it all the buttons are disabled and their texts have changed, so it is not working properly at all.

This is happening because you're trying to map all recycler view items with a single sharedprefs value each recycler items have individual id, hence to solve this you need to put an extra int value with your sharedprefs

Whenever you do sharedPref.putBoolean(Constants.CHANGE_INVITE_BUTTON, true) Instead of using Constants.CHANGE_INVITE_BUTTON change it to holder item value.

sharedPref.putBoolean(holder.binding.inviteToJobButtonFreelancerScreenCu.id, true)

Thereafter, change your if condition to this:

if(sharedPref.getBoolean(holder.binding.inviteToJobButtonFreelancerScreenCu.id,true))

Hence after this whenever your adapter is updated only the item id which is having true will be changed.

When you click on Invite Job Button you need to take one boolean variable that indicates you invite for that particular job or not in your model which you bind in your RecyclerView . And by default that boolean id false for all Jobs.

Once you click on Invite Job Button then you need to do that Boolean as true and notify your Adapter .

Text change and disable button you need to do based on your Boolean .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM