'Why doesn't my radioButton get checked or unchecked?

I followed a tutorial to check a single radioButton. I tried to change it so you can change multiple radioButtons. I succeeded to get the value out of the arraylist and show it on the screen, but when I click on it doesn't change. Only the radioButton that is unchecked get checked. Can anyone help me with this.


public interface ItemClickListener {
    //Create method
    void onClick(String s);
}

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    //Initialize variable
    RecyclerView recyclerView;
    ItemClickListener itemClickListener;
    MainAdapter adapter;
    ArrayList<String> arrayList;
    ArrayList<Boolean> arrayList_B;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Assign variable
        recyclerView = findViewById(R.id.recycler_view);
        fillArrayList();


        //Initialize listener
        itemClickListener = new ItemClickListener() {
            @Override
            public void onClick(String s) {
                //Notify adapter
                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
                //Display toast
                Toast.makeText(getApplicationContext(),
                        "Selected : " + s, Toast.LENGTH_SHORT).show();
            }
        };
        
        //Set layout manager
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //Initialize adapter
        adapter = new MainAdapter(arrayList, arrayList_B, itemClickListener);
        //Set adapter
        recyclerView.setAdapter(adapter);
    }

    public void fillArrayList() {
        //initialize array list
        arrayList = new ArrayList<>();
        arrayList_B = new ArrayList<>();

        //Use for loop
        for (int i = 0; i < 10; i++) {
            //Add values in array  list
            arrayList.add("RB " + i);
            arrayList_B.add(true);
        }
        arrayList_B.set(3,false);

    }
}


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
    //Initialize variable
    ArrayList<String> arrayList;
    ArrayList<Boolean> arrayList_B;
    ItemClickListener itemClickListener;
    int selectedPosition = -1;

    //Create constructor
   public MainAdapter(ArrayList<String> arrayList, ArrayList<Boolean> arrayList_B, ItemClickListener itemClickListener) {
        this.arrayList = arrayList;
        this.itemClickListener = itemClickListener;
        this.arrayList_B = arrayList_B;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //Initialize view
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_main, parent, false);

        //Pass holder view
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //Set text on radio button
        holder.radioButton.setText(arrayList.get(position));
        //Set true or false radio button
        holder.radioButton.setChecked(arrayList_B.get(position));

        //Set listener on radio button
        holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //When checked
                //Update selected position
                selectedPosition = holder.getAdapterPosition();

                //if (arrayList_B.get(selectedPosition).equals(true)) {
                //if (b == true) {
                //    arrayList_B.set(selectedPosition, false);
                //}
                //if (b == false) {
                    //
                //}
                arrayList_B.set(selectedPosition, b);
                holder.radioButton.setChecked(arrayList_B.get(selectedPosition));

                //Call listener
                //Get RadioButton name.
                itemClickListener.onClick(holder.radioButton.getText().toString());
                //set array list radiobutton
            }
        });
    }

    @Override
    public long getItemId(int position) {
       //Pass position
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        //Pass position
        return position;
    }

    @Override
    public int getItemCount() {
       //Pass total List size
        return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        //Initialize variable
        RadioButton radioButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            //Assign variable
            radioButton = itemView.findViewById(R.id.radiobutton);
        }
    }
}

The Layout: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view"
        tools:listitem="@layout/item_main"/>
</RelativeLayout>

The Layout: item_main.xml

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radiobutton"
    android:padding="12dp"
    android:textSize="18sp"
    android:textColor="@android:color/darker_gray"/>



Sources

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

Source: Stack Overflow

Solution Source