From RecyclerView
item, I am trying to save the selected RadioButton's text in model class. When I select a RadioButton
from 1st item, it's text is saved appropriately. Problem is, RadioButton's text at the same position from 8th item is also auto saved. If I select radio button from 2nd item, text from 9th item is also auto saved, and so on. How to solve this problem?
onBindViewHolder method is given below :
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
...
holder.radioGroup.setTag(position);
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) group.findViewById(radioButtonID);
int clickedPos = (Integer) group.getTag();
models.get(clickedPos).setChecked(radioButtonID);
if (radioButtonID > 0){
models.get(clickedPos).setSelectedAns(radioButton.getText().toString());
}
}
});
holder.radioGroup.check(models.get(position).getChecked());
Log.d("TAG", "At position " + position + " selected : " + models.get(position).getSelectedAns());
}