Cannot set id for radio buttons in ArrayAdapter with ListViewLazy load of images in ListViewHow to refresh Android listview?getView of ArrayAdapter inconsistent with image downloadingGet tags from Radiobuttons in ListView AndroidI am unable to uncheck radio buttons in listviewHow do I uncheck a radiogroup after a certain button is selected?Android - ListView with Radio buttonClear the radioGroup.getCheckedRadioButtonId() from Radio GroupRemove rows from listview on swipe or buttonRadioButton state and Button validation
Is there a good way to store credentials outside of a password manager?
Coordinate position not precise
Best way to store options for panels
Is there any reason not to eat food that's been dropped on the surface of the moon?
Opposite of a diet
The plural of 'stomach"
Personal Teleportation as a Weapon
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
What's the purpose of "true" in bash "if sudo true; then"
Was the picture area of a CRT a parallelogram (instead of a true rectangle)?
Displaying the order of the columns of a table
Can I use my Chinese passport to enter China after I acquired another citizenship?
How does it work when somebody invests in my business?
There is only s̶i̶x̶t̶y one place he can be
Can somebody explain Brexit in a few child-proof sentences?
Teaching indefinite integrals that require special-casing
How to verify if g is a generator for p?
Finding all intervals that match predicate in vector
voltage of sounds of mp3files
Irreducibility of a simple polynomial
How do I keep an essay about "feeling flat" from feeling flat?
Everything Bob says is false. How does he get people to trust him?
Is a roofing delivery truck likely to crack my driveway slab?
Is exact Kanji stroke length important?
Cannot set id for radio buttons in ArrayAdapter with ListView
Lazy load of images in ListViewHow to refresh Android listview?getView of ArrayAdapter inconsistent with image downloadingGet tags from Radiobuttons in ListView AndroidI am unable to uncheck radio buttons in listviewHow do I uncheck a radiogroup after a certain button is selected?Android - ListView with Radio buttonClear the radioGroup.getCheckedRadioButtonId() from Radio GroupRemove rows from listview on swipe or buttonRadioButton state and Button validation
[Sample project download link]
I'm having a tutorial quiz app working on. I have some questions and every question has three answers as 3 radio buttons inside a radio group. The problem is when I use setId for radio buttons i get answers replaced so question number 1 gets answers of questions number 5 and so on which makes everything wrong. when I don't setId for radio buttons I get answers placed correctly for everyquestion but the problem now is when I submit the answer of a question it sumbits another question too because radio buttons don't have ids to catch while listening to the submit button click.
I tried to setId again but the same issue is replacing answers between questions. How can I specify different id for each radio button? I tried to make randon number but it's duplicated for the first hidden question when scrolling down.
public class QuestionsAdapter extends ArrayAdapter<Questions>
public QuestionsAdapter(Activity context, ArrayList<Questions> questions)
super(context, 0, questions);
private int t;
Random ran = new Random();
// Assumes max and min are non-negative.
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
//styling odd and even items
if (position % 2 == 1)
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.odd));
else
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.even));
//used ViewHolder to prevent triggering position null issue
final ViewHolder viewHolder = new ViewHolder();
try
final Questions currentQuestion = getItem(position);
String question = currentQuestion.getQuestionTitle();
String option_1 = currentQuestion.getQuestionAnswer1();
String option_2 = currentQuestion.getQuestionAnswer2();
String option_3 = currentQuestion.getQuestionAnswer3();
int q_id = currentQuestion.getQID();
viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
viewHolder.questionTextView.setText(question);
viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);
viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
viewHolder.firstAnswer.setText(option_1);
viewHolder.secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
viewHolder.secondAnswer.setText(option_2);
viewHolder.thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
viewHolder.thirdAnswer.setText(option_3);
final TextView resultTextView = (TextView) listItemView.findViewById(R.id.result_text);
final Button viewAnswer = (Button) listItemView.findViewById(R.id.view_answer_btn);
viewAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if(viewHolder.radioGroup.getCheckedRadioButtonId()!=-1)
//getting checked radio button and save it's text in a string
int selectedAnswerId = viewHolder.radioGroup.getCheckedRadioButtonId();
View radioButton = viewHolder.radioGroup.findViewById(selectedAnswerId);
int radioId = viewHolder.radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) viewHolder.radioGroup.getChildAt(radioId);
String selection = (String) btn.getText();
//displaying the empty result TextView
resultTextView.setVisibility(View.VISIBLE);
//if answer is correct (selection equals the answer in the array
if( selection == currentQuestion.getCorrect() )
//increasing score by 1
MainActivity.score += 1;
//displaying the score
MainActivity.scoreTextView.setText(getContext().getResources().getString(R.string.score_is) +
MainActivity.score + "/" + MainActivity.arraySize);
//displaying the result after submitting the answer of this question
resultTextView.setText(selection + " " + getContext().getResources().getString(R.string.correct));
//changing color of the result to green
resultTextView.setTextColor(getContext().getResources().getColor(R.color.green));
else
//displaying the result after submitting the answer of this question
resultTextView.setText(getContext().getResources().getString(R.string.wrong_answer) + " " +
currentQuestion.getCorrect());
//changing color of the result to red
resultTextView.setTextColor(getContext().getResources().getColor(R.color.red));
//hiding the button and RadioGroup of this question
viewHolder.radioGroup.setVisibility(View.GONE);
viewAnswer.setVisibility(View.GONE);
else
Toast.makeText(getContext(),getContext().getResources().getString(R.string.Choose_answer_first),
Toast.LENGTH_SHORT).show();
);
catch (Exception e)
e.printStackTrace();
return listItemView;
private class ViewHolder
TextView questionTextView;
RadioGroup radioGroup;
RadioButton firstAnswer, secondAnswer,thirdAnswer;
android android-listview android-arrayadapter
add a comment |
[Sample project download link]
I'm having a tutorial quiz app working on. I have some questions and every question has three answers as 3 radio buttons inside a radio group. The problem is when I use setId for radio buttons i get answers replaced so question number 1 gets answers of questions number 5 and so on which makes everything wrong. when I don't setId for radio buttons I get answers placed correctly for everyquestion but the problem now is when I submit the answer of a question it sumbits another question too because radio buttons don't have ids to catch while listening to the submit button click.
I tried to setId again but the same issue is replacing answers between questions. How can I specify different id for each radio button? I tried to make randon number but it's duplicated for the first hidden question when scrolling down.
public class QuestionsAdapter extends ArrayAdapter<Questions>
public QuestionsAdapter(Activity context, ArrayList<Questions> questions)
super(context, 0, questions);
private int t;
Random ran = new Random();
// Assumes max and min are non-negative.
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
//styling odd and even items
if (position % 2 == 1)
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.odd));
else
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.even));
//used ViewHolder to prevent triggering position null issue
final ViewHolder viewHolder = new ViewHolder();
try
final Questions currentQuestion = getItem(position);
String question = currentQuestion.getQuestionTitle();
String option_1 = currentQuestion.getQuestionAnswer1();
String option_2 = currentQuestion.getQuestionAnswer2();
String option_3 = currentQuestion.getQuestionAnswer3();
int q_id = currentQuestion.getQID();
viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
viewHolder.questionTextView.setText(question);
viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);
viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
viewHolder.firstAnswer.setText(option_1);
viewHolder.secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
viewHolder.secondAnswer.setText(option_2);
viewHolder.thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
viewHolder.thirdAnswer.setText(option_3);
final TextView resultTextView = (TextView) listItemView.findViewById(R.id.result_text);
final Button viewAnswer = (Button) listItemView.findViewById(R.id.view_answer_btn);
viewAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if(viewHolder.radioGroup.getCheckedRadioButtonId()!=-1)
//getting checked radio button and save it's text in a string
int selectedAnswerId = viewHolder.radioGroup.getCheckedRadioButtonId();
View radioButton = viewHolder.radioGroup.findViewById(selectedAnswerId);
int radioId = viewHolder.radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) viewHolder.radioGroup.getChildAt(radioId);
String selection = (String) btn.getText();
//displaying the empty result TextView
resultTextView.setVisibility(View.VISIBLE);
//if answer is correct (selection equals the answer in the array
if( selection == currentQuestion.getCorrect() )
//increasing score by 1
MainActivity.score += 1;
//displaying the score
MainActivity.scoreTextView.setText(getContext().getResources().getString(R.string.score_is) +
MainActivity.score + "/" + MainActivity.arraySize);
//displaying the result after submitting the answer of this question
resultTextView.setText(selection + " " + getContext().getResources().getString(R.string.correct));
//changing color of the result to green
resultTextView.setTextColor(getContext().getResources().getColor(R.color.green));
else
//displaying the result after submitting the answer of this question
resultTextView.setText(getContext().getResources().getString(R.string.wrong_answer) + " " +
currentQuestion.getCorrect());
//changing color of the result to red
resultTextView.setTextColor(getContext().getResources().getColor(R.color.red));
//hiding the button and RadioGroup of this question
viewHolder.radioGroup.setVisibility(View.GONE);
viewAnswer.setVisibility(View.GONE);
else
Toast.makeText(getContext(),getContext().getResources().getString(R.string.Choose_answer_first),
Toast.LENGTH_SHORT).show();
);
catch (Exception e)
e.printStackTrace();
return listItemView;
private class ViewHolder
TextView questionTextView;
RadioGroup radioGroup;
RadioButton firstAnswer, secondAnswer,thirdAnswer;
android android-listview android-arrayadapter
add a comment |
[Sample project download link]
I'm having a tutorial quiz app working on. I have some questions and every question has three answers as 3 radio buttons inside a radio group. The problem is when I use setId for radio buttons i get answers replaced so question number 1 gets answers of questions number 5 and so on which makes everything wrong. when I don't setId for radio buttons I get answers placed correctly for everyquestion but the problem now is when I submit the answer of a question it sumbits another question too because radio buttons don't have ids to catch while listening to the submit button click.
I tried to setId again but the same issue is replacing answers between questions. How can I specify different id for each radio button? I tried to make randon number but it's duplicated for the first hidden question when scrolling down.
public class QuestionsAdapter extends ArrayAdapter<Questions>
public QuestionsAdapter(Activity context, ArrayList<Questions> questions)
super(context, 0, questions);
private int t;
Random ran = new Random();
// Assumes max and min are non-negative.
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
//styling odd and even items
if (position % 2 == 1)
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.odd));
else
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.even));
//used ViewHolder to prevent triggering position null issue
final ViewHolder viewHolder = new ViewHolder();
try
final Questions currentQuestion = getItem(position);
String question = currentQuestion.getQuestionTitle();
String option_1 = currentQuestion.getQuestionAnswer1();
String option_2 = currentQuestion.getQuestionAnswer2();
String option_3 = currentQuestion.getQuestionAnswer3();
int q_id = currentQuestion.getQID();
viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
viewHolder.questionTextView.setText(question);
viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);
viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
viewHolder.firstAnswer.setText(option_1);
viewHolder.secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
viewHolder.secondAnswer.setText(option_2);
viewHolder.thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
viewHolder.thirdAnswer.setText(option_3);
final TextView resultTextView = (TextView) listItemView.findViewById(R.id.result_text);
final Button viewAnswer = (Button) listItemView.findViewById(R.id.view_answer_btn);
viewAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if(viewHolder.radioGroup.getCheckedRadioButtonId()!=-1)
//getting checked radio button and save it's text in a string
int selectedAnswerId = viewHolder.radioGroup.getCheckedRadioButtonId();
View radioButton = viewHolder.radioGroup.findViewById(selectedAnswerId);
int radioId = viewHolder.radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) viewHolder.radioGroup.getChildAt(radioId);
String selection = (String) btn.getText();
//displaying the empty result TextView
resultTextView.setVisibility(View.VISIBLE);
//if answer is correct (selection equals the answer in the array
if( selection == currentQuestion.getCorrect() )
//increasing score by 1
MainActivity.score += 1;
//displaying the score
MainActivity.scoreTextView.setText(getContext().getResources().getString(R.string.score_is) +
MainActivity.score + "/" + MainActivity.arraySize);
//displaying the result after submitting the answer of this question
resultTextView.setText(selection + " " + getContext().getResources().getString(R.string.correct));
//changing color of the result to green
resultTextView.setTextColor(getContext().getResources().getColor(R.color.green));
else
//displaying the result after submitting the answer of this question
resultTextView.setText(getContext().getResources().getString(R.string.wrong_answer) + " " +
currentQuestion.getCorrect());
//changing color of the result to red
resultTextView.setTextColor(getContext().getResources().getColor(R.color.red));
//hiding the button and RadioGroup of this question
viewHolder.radioGroup.setVisibility(View.GONE);
viewAnswer.setVisibility(View.GONE);
else
Toast.makeText(getContext(),getContext().getResources().getString(R.string.Choose_answer_first),
Toast.LENGTH_SHORT).show();
);
catch (Exception e)
e.printStackTrace();
return listItemView;
private class ViewHolder
TextView questionTextView;
RadioGroup radioGroup;
RadioButton firstAnswer, secondAnswer,thirdAnswer;
android android-listview android-arrayadapter
[Sample project download link]
I'm having a tutorial quiz app working on. I have some questions and every question has three answers as 3 radio buttons inside a radio group. The problem is when I use setId for radio buttons i get answers replaced so question number 1 gets answers of questions number 5 and so on which makes everything wrong. when I don't setId for radio buttons I get answers placed correctly for everyquestion but the problem now is when I submit the answer of a question it sumbits another question too because radio buttons don't have ids to catch while listening to the submit button click.
I tried to setId again but the same issue is replacing answers between questions. How can I specify different id for each radio button? I tried to make randon number but it's duplicated for the first hidden question when scrolling down.
public class QuestionsAdapter extends ArrayAdapter<Questions>
public QuestionsAdapter(Activity context, ArrayList<Questions> questions)
super(context, 0, questions);
private int t;
Random ran = new Random();
// Assumes max and min are non-negative.
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
//styling odd and even items
if (position % 2 == 1)
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.odd));
else
listItemView.setBackgroundColor(getContext().getResources().getColor(R.color.even));
//used ViewHolder to prevent triggering position null issue
final ViewHolder viewHolder = new ViewHolder();
try
final Questions currentQuestion = getItem(position);
String question = currentQuestion.getQuestionTitle();
String option_1 = currentQuestion.getQuestionAnswer1();
String option_2 = currentQuestion.getQuestionAnswer2();
String option_3 = currentQuestion.getQuestionAnswer3();
int q_id = currentQuestion.getQID();
viewHolder.questionTextView = (TextView) listItemView.findViewById(R.id.q_text);
viewHolder.questionTextView.setText(question);
viewHolder.radioGroup = (RadioGroup) listItemView.findViewById(R.id.q_answers);
viewHolder.firstAnswer = (RadioButton) listItemView.findViewById(R.id.q_option1);
viewHolder.firstAnswer.setText(option_1);
viewHolder.secondAnswer = (RadioButton) listItemView.findViewById(R.id.q_option2);
viewHolder.secondAnswer.setText(option_2);
viewHolder.thirdAnswer = (RadioButton) listItemView.findViewById(R.id.q_option3);
viewHolder.thirdAnswer.setText(option_3);
final TextView resultTextView = (TextView) listItemView.findViewById(R.id.result_text);
final Button viewAnswer = (Button) listItemView.findViewById(R.id.view_answer_btn);
viewAnswer.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if(viewHolder.radioGroup.getCheckedRadioButtonId()!=-1)
//getting checked radio button and save it's text in a string
int selectedAnswerId = viewHolder.radioGroup.getCheckedRadioButtonId();
View radioButton = viewHolder.radioGroup.findViewById(selectedAnswerId);
int radioId = viewHolder.radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) viewHolder.radioGroup.getChildAt(radioId);
String selection = (String) btn.getText();
//displaying the empty result TextView
resultTextView.setVisibility(View.VISIBLE);
//if answer is correct (selection equals the answer in the array
if( selection == currentQuestion.getCorrect() )
//increasing score by 1
MainActivity.score += 1;
//displaying the score
MainActivity.scoreTextView.setText(getContext().getResources().getString(R.string.score_is) +
MainActivity.score + "/" + MainActivity.arraySize);
//displaying the result after submitting the answer of this question
resultTextView.setText(selection + " " + getContext().getResources().getString(R.string.correct));
//changing color of the result to green
resultTextView.setTextColor(getContext().getResources().getColor(R.color.green));
else
//displaying the result after submitting the answer of this question
resultTextView.setText(getContext().getResources().getString(R.string.wrong_answer) + " " +
currentQuestion.getCorrect());
//changing color of the result to red
resultTextView.setTextColor(getContext().getResources().getColor(R.color.red));
//hiding the button and RadioGroup of this question
viewHolder.radioGroup.setVisibility(View.GONE);
viewAnswer.setVisibility(View.GONE);
else
Toast.makeText(getContext(),getContext().getResources().getString(R.string.Choose_answer_first),
Toast.LENGTH_SHORT).show();
);
catch (Exception e)
e.printStackTrace();
return listItemView;
private class ViewHolder
TextView questionTextView;
RadioGroup radioGroup;
RadioButton firstAnswer, secondAnswer,thirdAnswer;
android android-listview android-arrayadapter
android android-listview android-arrayadapter
edited Mar 21 at 16:31
PHP User
asked Mar 21 at 15:23
PHP UserPHP User
76421439
76421439
add a comment |
add a comment |
0
active
oldest
votes
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%2f55283836%2fcannot-set-id-for-radio-buttons-in-arrayadapter-with-listview%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f55283836%2fcannot-set-id-for-radio-buttons-in-arrayadapter-with-listview%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