Java: get Information from Adapter to Activity if any radio button is clicked or not

antsakontsa :

I cannot get information about the checked radio button from my Adapter to activity

This is the item, in that recyclerview, where the radio button is located:

private boolean mRadioButton;
private String mCourseName, mHolesTxt, mHolesNm, mParTxt, mParNm;

public NewGameCourseItem(boolean radioButton, String courseName, String holesTxt, String holesNm, String parTxt, String parNm) {
    mRadioButton = radioButton;
    mCourseName = courseName;
    mHolesTxt = holesTxt;
    mHolesNm = holesNm;
    mParTxt = parTxt;
    mParNm = parNm;
}

public boolean getRadioButton() {
    return mRadioButton;
}

public String getCourseName() {
    return mCourseName;
}

public String getHolesTxt() {
    return mHolesTxt;
}

public String getHolesNm() {
    return mHolesNm;
}

public String getParTxt() {
    return mParTxt;
}

public String getParNm() {
    return mParNm;
}

Here is my adapter, that keeps on track which radio button is selected:

public class ActivityNewGame2 extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private NewGameCourseAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private ArrayList<NewGameCourseItem> mCourseList;

@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 isChecked) {
            selectedPosition = holder.getAdapterPosition();

            /** This part considers checking for radio buttons (checked or not) **/
            if (selectedPosition == position) {
                holder.mRadioButton.setChecked(true);
                notifyDataSetChanged();
            } else {
                holder.mRadioButton.setChecked(false);
                notifyDataSetChanged();
            }

Here is my Activity where I'm trying to check that list, where those radio buttons are, and also if any of those radio buttons are true, then when "start game" button been clicked, intent to another activity:

    mStartGame = findViewById(R.id.button_start_game);

    mStartGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i = 0; i < mCourseList.size(); i++) {
                /** If any radio button is selected, then intent to another Activity **/
                if (mCourseList.get(i).getRadioButton() == true) {
                    Intent intent = new Intent(ActivityNewGame2.this, ActivityGame.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                }
            }

        }
    });

At the moment, it won't intent to another activity... When I launch my app and click different radio buttons, they select correctly tho...

Naveen :

Problem is here you are not updating you object class when checkbox selected try this:

in your adaper:

if (selectedPosition == position) {
            holder.mRadioButton.setChecked(true);

          yourList.get(position).setRadioButton(holder.mRadioButton.isChecked)
            notifyDataSetChanged();
        } else {
            holder.mRadioButton.setChecked(false);
         yourList.get(position).setRadioButton(holder.mRadioButton.isChecked)
            notifyDataSetChanged();
        }

in Your NewGameCourseItem class add below method

public void setRadioButton(boolen isChecked) {
this.mRadioButton = isChecked;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154485&siteId=1