package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.CheckBox;

import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.List;
import java.util.Set;

public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.ViewHolder> {

    private List<String> currencies;
    private Set<String> favoriteCurrencies;
    private OnFavoriteClickListener listener;

    public interface OnFavoriteClickListener {
        void onFavoriteClick(String currency, boolean isFavorite);
    }

    public CurrencyAdapter(List<String> currencies, Set<String> favoriteCurrencies, OnFavoriteClickListener listener) {
        this.currencies = currencies;
        this.favoriteCurrencies = favoriteCurrencies;
        this.listener = listener;
    }

    @Override
    public CurrencyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.currency_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CurrencyAdapter.ViewHolder holder, int position) {
        String currency = currencies.get(position);
        holder.currencyName.setText(currency);

        // Убираем слушатель перед сменой состояния
        holder.favoriteCheckBox.setOnCheckedChangeListener(null);
        holder.favoriteCheckBox.setChecked(favoriteCurrencies.contains(currency));

        // Устанавливаем слушатель
        holder.favoriteCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (listener != null) {
                listener.onFavoriteClick(currency, isChecked);
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView currencyName;
        public CheckBox favoriteCheckBox;

        public ViewHolder(View itemView) {
            super(itemView);
            currencyName = itemView.findViewById(R.id.currency_name);
            favoriteCheckBox = itemView.findViewById(R.id.favorite_checkbox);
        }
    }
} <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_currency_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CurrencyListActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/currency_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout> добавь кнопку назад в mainActivity