'how to save the button state recyclerview

Good days, I am writing an android program for online coupon service. I am currently creating a page that allows user to bookmark the item. Then after that they can see it in their bookmarks folder. The problem I am encountering is after the bookmark is clicked and selected for the specific item. but when i scroll up the selected bookmark becomes deselected state. How can I prevent that from happening. Below is my code

Coupon.java

public class Coupon {
    private String company_name;
    private String offer_desc;


    public Coupon() {

    }

    public Coupon(String company_name, String offer_desc) {
        this.company_name = company_name;
        this.offer_desc = offer_desc;
    }

    public String getCompany_name() {
        return company_name;
    }

    public void setCompany_name(String company_name) {
        this.company_name = company_name;
    }

    public String getOffer_desc() {
        return offer_desc;
    }

    public void setOffer_desc(String offer_desc) {
        this.offer_desc = offer_desc;
    } 


}

CouponViewHolder.java

public class CouponViewHolder extends RecyclerView.ViewHolder{
    protected TextView company_name;
    protected TextView offer_desc;
    protected LikeButton star_button;
    protected LikeButton heart_button;

    public CouponViewHolder(final View item){
        super(item);
        company_name = (TextView) item.findViewById(R.id.company_name);
        offer_desc = (TextView) item.findViewById(R.id.offer_desc);
        star_button = (LikeButton) item.findViewById(R.id.star_button);
        heart_button = (LikeButton) item.findViewById(R.id.heart_button);
    }

}

CouponAdapter.java

public class CouponAdapter extends RecyclerView.Adapter<CouponViewHolder>{

    private List<Coupon> couponList;

    public CouponAdapter(List<Coupon> couponList) {
        this.couponList = couponList;
    }


    @Override
    public CouponViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_one_card, parent, false);

        CouponViewHolder vh = new CouponViewHolder(v);
        return vh;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(CouponViewHolder holder, int position) {
        Coupon coupon = couponList.get(position);
        holder.company_name.setText(coupon.getCompany_name());
        holder.offer_desc.setText(coupon.getOffer_desc());
        holder.heart_button.setLiked(false);
        holder.star_button.setLiked(false);

        holder.heart_button.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                notifyDataSetChanged();
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return couponList.size();
    }

}

Thank you



Solution 1:[1]

You have to save the selected things on database or sharedpreferences or whatever saving method you use. you can create an interface for making this action on your class or activity.

Solution 2:[2]

1-For the most cases saving the state of the button in the model list is enough. However,when an user change its fragment or activity, there is possibility of that you model list could be destroyed by the system because of memory usage. For this case you have choose whether persist your data using sharedpref,file,db etc or keeping your data in a store whose lifespan is longer than the activity or fragment. It seems that the page you share is related to your app's main functionality so you should keep your data in a repository whose scope is dependent on the app scope. I recommend you to look at mvvm architecture and repository pattern.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Castro Alhdo
Solution 2 Ahmed elghetany