I have a RecyclerView that displays a list of RadioButtons of countries, to let the user choose his country. I don't want more than on Item clicked, so I've wrote this OnCheckListener in the onBindView method of the adapter, but whenever I click a RadioButton, several other buttons toggles with it.
@Override
public void onBindViewHolder(final com.example.ameer.ta7adialma3rifa.adapters.CountriesAdapter.ViewHolder holder, final int position) {
String name = mData.get(position).getName();
int imgId = mData.get(position).getImageId();
holder.radioButton.setText(name);
holder.flagImageView.setImageResource(imgId);
holder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
RadioButton button = (RadioButton) compoundButton;
if (!button.getText().equals(mData.get(position).getName())){
button.setChecked(false);
}
if (runnable != null){
runnable.run();
}
runnable = new Runnable() {
@Override
public void run() {
holder.radioButton.setChecked(false);
}
};
}
});
}
You should put your radio buttons in a radio group. this way just one of them is being chosen. and I don't know why are using runnable to set the radio button checked! I hope this helps you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.