0

In case any 1 find this problem, I hope this helps some1 in the future. I wanted to share the final solution because I think it helps more than the code I started with and couldn't get any answer which led to this solution.

This was a reference for my solution: How can I select only one checkbox in Recyclerview and notifydataset changed

I'm only sharing crucial parts of the code, which affect directly to the radio button selection problem.

this was my solution:

public class NewGameCourseAdapter extends RecyclerView.Adapter<NewGameCourseAdapter.NewGameCourseViewHolder> {
    private int selectedPosition = -1;// no selection by default

    @Override
    public void onBindViewHolder(@NonNull final NewGameCourseViewHolder holder, final int position) {

        /** This can prevent some unwanted actions in some cases **/
        holder.mRadioButton.setOnCheckedChangeListener(null);

        holder.mRadioButton.setChecked(selectedPosition == position);

        holder.mRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectedPosition = holder.getAdapterPosition();

                if (selectedPosition == position) {
                    holder.mRadioButton.setChecked(true);
                    notifyDataSetChanged();
                } else {
                    holder.mRadioButton.setChecked(false);
                    notifyDataSetChanged();
                }
            }
        });
  • Can you explain which tech do you use? javafx/swing/ something else? – Anton Petrov Jul 18 '19 at 18:09
  • I Edited header – Antti Kuisma Jul 18 '19 at 18:15
  • That's not enough... and tags should't be put in the title. What ui framework are you working with? – Modus Tollens Jul 18 '19 at 18:29
  • You should find something line a radio group or radio button group in its api which should handle the "one radio button at a time" part. – Modus Tollens Jul 18 '19 at 18:31
  • Apparently this is an android. You have to use `android.widget.RadioGroup` to create group of buttons. – Anton Petrov Jul 18 '19 at 18:36
  • Changed my post – Antti Kuisma Jul 19 '19 at 04:42
  • take a look at this https://developer.android.com/guide/topics/ui/controls/radiobutton for reference. – user3678528 Jul 19 '19 at 04:51
  • I've been thinking, how can I put radio buttons in a radio group in recyclerview, because I have 1 layout (item) where I define radiobutton, if I put that in radiogroup, wouldn't that mean that every item is their own individual group, so they are never all in 1 group? – Antti Kuisma Jul 19 '19 at 04:58
  • Take a read of the documentation, there would be something on radio groups (which you will be using) If that does not work, re evaluate your work because you have taken a left turn somewhere where you should have taken a right. – Spangle Jul 19 '19 at 05:21

0 Answers0