Clicking RadioButton “A” toggles RadioButton “B” RecyclerView
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);
}
};
}
});
}
java android android-recyclerview radio-button recycler-adapter
|
show 1 more comment
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);
}
};
}
});
}
java android android-recyclerview radio-button recycler-adapter
what is the use of runnable ??
– Munir
Dec 1 '17 at 16:44
Remove the Runnable. Since yourholder.radioButton.setChecked(false);
runs on Runnable, it messes with the recyclerview item's position.
– Vihanga Yasith
Dec 1 '17 at 16:47
The runnable is used to uncheck the previous checked button, so when I click a button, it will uncheck the one selected before, to limit the checked buttons to one at a time.
– Ameer Taweel
Dec 1 '17 at 16:47
It will not work as you expected. Try to run a for-loop and remove the checked state of other items.
– Vihanga Yasith
Dec 1 '17 at 16:50
I don't understand your logic. But remember that viewholder reuses view. For example if your 4th radioButton was checked after some intervals your next radioButton also will be checked. Be carefull in if statements in viewholder. if you put an if statement then put else statement also.
– Bek
Dec 1 '17 at 16:59
|
show 1 more comment
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);
}
};
}
});
}
java android android-recyclerview radio-button recycler-adapter
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);
}
};
}
});
}
java android android-recyclerview radio-button recycler-adapter
java android android-recyclerview radio-button recycler-adapter
edited Dec 1 '17 at 16:48
Fantômas
32.7k156389
32.7k156389
asked Dec 1 '17 at 16:40
Ameer TaweelAmeer Taweel
300215
300215
what is the use of runnable ??
– Munir
Dec 1 '17 at 16:44
Remove the Runnable. Since yourholder.radioButton.setChecked(false);
runs on Runnable, it messes with the recyclerview item's position.
– Vihanga Yasith
Dec 1 '17 at 16:47
The runnable is used to uncheck the previous checked button, so when I click a button, it will uncheck the one selected before, to limit the checked buttons to one at a time.
– Ameer Taweel
Dec 1 '17 at 16:47
It will not work as you expected. Try to run a for-loop and remove the checked state of other items.
– Vihanga Yasith
Dec 1 '17 at 16:50
I don't understand your logic. But remember that viewholder reuses view. For example if your 4th radioButton was checked after some intervals your next radioButton also will be checked. Be carefull in if statements in viewholder. if you put an if statement then put else statement also.
– Bek
Dec 1 '17 at 16:59
|
show 1 more comment
what is the use of runnable ??
– Munir
Dec 1 '17 at 16:44
Remove the Runnable. Since yourholder.radioButton.setChecked(false);
runs on Runnable, it messes with the recyclerview item's position.
– Vihanga Yasith
Dec 1 '17 at 16:47
The runnable is used to uncheck the previous checked button, so when I click a button, it will uncheck the one selected before, to limit the checked buttons to one at a time.
– Ameer Taweel
Dec 1 '17 at 16:47
It will not work as you expected. Try to run a for-loop and remove the checked state of other items.
– Vihanga Yasith
Dec 1 '17 at 16:50
I don't understand your logic. But remember that viewholder reuses view. For example if your 4th radioButton was checked after some intervals your next radioButton also will be checked. Be carefull in if statements in viewholder. if you put an if statement then put else statement also.
– Bek
Dec 1 '17 at 16:59
what is the use of runnable ??
– Munir
Dec 1 '17 at 16:44
what is the use of runnable ??
– Munir
Dec 1 '17 at 16:44
Remove the Runnable. Since your
holder.radioButton.setChecked(false);
runs on Runnable, it messes with the recyclerview item's position.– Vihanga Yasith
Dec 1 '17 at 16:47
Remove the Runnable. Since your
holder.radioButton.setChecked(false);
runs on Runnable, it messes with the recyclerview item's position.– Vihanga Yasith
Dec 1 '17 at 16:47
The runnable is used to uncheck the previous checked button, so when I click a button, it will uncheck the one selected before, to limit the checked buttons to one at a time.
– Ameer Taweel
Dec 1 '17 at 16:47
The runnable is used to uncheck the previous checked button, so when I click a button, it will uncheck the one selected before, to limit the checked buttons to one at a time.
– Ameer Taweel
Dec 1 '17 at 16:47
It will not work as you expected. Try to run a for-loop and remove the checked state of other items.
– Vihanga Yasith
Dec 1 '17 at 16:50
It will not work as you expected. Try to run a for-loop and remove the checked state of other items.
– Vihanga Yasith
Dec 1 '17 at 16:50
I don't understand your logic. But remember that viewholder reuses view. For example if your 4th radioButton was checked after some intervals your next radioButton also will be checked. Be carefull in if statements in viewholder. if you put an if statement then put else statement also.
– Bek
Dec 1 '17 at 16:59
I don't understand your logic. But remember that viewholder reuses view. For example if your 4th radioButton was checked after some intervals your next radioButton also will be checked. Be carefull in if statements in viewholder. if you put an if statement then put else statement also.
– Bek
Dec 1 '17 at 16:59
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
Actually I need to use the recyclerView because there is a lot of countries and if use RadioGroup it will lead to outOfMemoryExeption
– Ameer Taweel
Dec 1 '17 at 18:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47597718%2fclicking-radiobutton-a-toggles-radiobutton-b-recyclerview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Actually I need to use the recyclerView because there is a lot of countries and if use RadioGroup it will lead to outOfMemoryExeption
– Ameer Taweel
Dec 1 '17 at 18:25
add a comment |
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.
Actually I need to use the recyclerView because there is a lot of countries and if use RadioGroup it will lead to outOfMemoryExeption
– Ameer Taweel
Dec 1 '17 at 18:25
add a comment |
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.
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.
answered Dec 1 '17 at 18:01
MaHDiMaHDi
777
777
Actually I need to use the recyclerView because there is a lot of countries and if use RadioGroup it will lead to outOfMemoryExeption
– Ameer Taweel
Dec 1 '17 at 18:25
add a comment |
Actually I need to use the recyclerView because there is a lot of countries and if use RadioGroup it will lead to outOfMemoryExeption
– Ameer Taweel
Dec 1 '17 at 18:25
Actually I need to use the recyclerView because there is a lot of countries and if use RadioGroup it will lead to outOfMemoryExeption
– Ameer Taweel
Dec 1 '17 at 18:25
Actually I need to use the recyclerView because there is a lot of countries and if use RadioGroup it will lead to outOfMemoryExeption
– Ameer Taweel
Dec 1 '17 at 18:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47597718%2fclicking-radiobutton-a-toggles-radiobutton-b-recyclerview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what is the use of runnable ??
– Munir
Dec 1 '17 at 16:44
Remove the Runnable. Since your
holder.radioButton.setChecked(false);
runs on Runnable, it messes with the recyclerview item's position.– Vihanga Yasith
Dec 1 '17 at 16:47
The runnable is used to uncheck the previous checked button, so when I click a button, it will uncheck the one selected before, to limit the checked buttons to one at a time.
– Ameer Taweel
Dec 1 '17 at 16:47
It will not work as you expected. Try to run a for-loop and remove the checked state of other items.
– Vihanga Yasith
Dec 1 '17 at 16:50
I don't understand your logic. But remember that viewholder reuses view. For example if your 4th radioButton was checked after some intervals your next radioButton also will be checked. Be carefull in if statements in viewholder. if you put an if statement then put else statement also.
– Bek
Dec 1 '17 at 16:59