Reputation: 1042
@Override
public void onBindViewHolder(@NonNull StateViewHolder stateViewHolder, int i) {
GetStateApiResponse getStateApiResponse = state.get(i);
stateViewHolder.btnstate.setText(getStateApiResponse.getStateName().toString());
stateViewHolder.disttCount.setText(getStateApiResponse.getCount().toString());
stateViewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checked_rb = group.findViewById(checkedId);
if (lastChecked != null) {
lastChecked.setChecked(false);
}
//store the clicked radiobutton
lastChecked = checked_rb;
}
});
}
this code is not working corrently on recyclerview if I clicked on radio button than automatically unchecked
Upvotes: 2
Views: 191
Reputation: 1042
correct answer of this question
private RadioButton lastChecked
...
@Override
public void onBindViewHolder(@NonNull StateViewHolder stateViewHolder, int i) {
GetStateApiResponse getStateApiResponse = state.get(i);
stateViewHolder.btnstate.setText(getStateApiResponse.getStateName().toString());
stateViewHolder.disttCount.setText(getStateApiResponse.getCount().toString());
if (lastChecked!=null && lastCheckedPos!=-1 && i==lastCheckedPos)
{
lastChecked.setChecked(true);
}
else
{
stateViewHolder.btnstate.setChecked(false);
}
stateViewHolder.btnstate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(GetStateApiResponse tempModel: state)
tempModel.setSetchecked(false);
state.get(i).setSetchecked(true);
lastCheckedPos=i;
if(null != lastChecked && !v.equals(lastChecked))
lastChecked.setChecked(false);
lastChecked = (RadioButton) v;
lastChecked.setChecked(true);
}
});
}
Upvotes: 3
Reputation: 543
I think that is because lastChecked
is an instance variable and holds the state of all of your list items. You can solve this problem by specifying a lastChecked
variable for each of your items in the list.
Upvotes: 1