0

How to get the position of particular checkbox(yes or no) in each row of a recycler view I am able to get the position of row wise checkbox but on scrolling both (yes and no) checkbox are getting checked.

((CheckBoxViewHolder) holder).chkNo.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((CheckBoxViewHolder) holder).chkYes.setChecked(false);
                    CheckBox cb = (CheckBox) v;
                    RatingQues contact = (RatingQues) cb.getTag();
                    contact.setSelected(cb.isChecked());
                    ratingQuestions.get(position).setSelected(cb.isChecked());


            ((CheckBoxViewHolder) holder).chkYes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((CheckBoxViewHolder) holder).chkNo.setChecked(false);
                   CheckBox cb = (CheckBox) v;
                    RatingQues ratingQues = (RatingQues) cb.getTag();
                     ratingQues.setSelected(cb.isChecked());
                    ratingQuestions.get(position).setSelected(cb.isChecked());

3 Answers3

2

Maintain a model for the current status, for example -

class MyModel{
    String status;
    boolean isChecked;
}

Now tag position with every checkbox, in onBind. So whenever you getChangeListner just change in model. Also bind checkbox as per model flag value.

Hope it will help, let me know if you need further help :)

Neo
  • 3,546
  • 1
  • 24
  • 31
  • Okay, first do one change, set tag position only, so once you have the position you can easily access the question and all thing. Now on chngeListener of yes, get tagged position from view and change the model `ratingQuestion.get(position).isSelected = true;` and on click of no set as false. hope it will help you :) – Neo Jul 04 '16 at 11:02
  • Did you tried my solution? No need to change the model. – Neo Jul 04 '16 at 11:32
  • And if you need help in code, kindly edit your post and add your adapter code. – Neo Jul 04 '16 at 11:33
  • radio button will work in this situation *bold*`chHolder.radioGroup.setTag(newInteger(position));chHolder.rGrp.check(questions.get(position).getSelectedRadioButtonId());chHolder.rGrp.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {@Override public void onCheckedChanged(RadioGroup group, int checkedId) {int radioBtnID = group.getCheckedRadioButtonId();selectedPos = (Integer) group.getTag(); questions.get(position).setSelected(true); questions.get(selectedPos).setSelectedRadioButtonId(radioBtnID);}});chHolder.rGrp.check(questions.get(position).getSelectedRadioButtonId());` –  Jul 12 '16 at 09:20
0

See this answer you can go with two Boolean variable and instead od visibility use setchecked(true/false)

Community
  • 1
  • 1
Jinal Patel
  • 699
  • 5
  • 15
0

You can use a SparseArray to record which one you have already checked.

秃德666
  • 395
  • 1
  • 3
  • 15