[示例项目download link]
我正在使用一个教程测验应用程序。我有一些问题,每个问题都有三个答案,即单选组中的3个单选按钮。问题是,当我对单选按钮使用setId时,我得到了替换的答案,所以问题1得到了问题5的答案,依此类推,这一切都错了。当我没有为单选按钮设置setId时,我会为每个问题正确放置答案,但是现在的问题是,当我提交问题的答案时,它也会引起另一个问题,因为单选按钮在收听提交按钮时没有id可以捕捉点击。
我尝试再次设置setId,但相同的问题是替换问题之间的答案。如何为每个单选按钮指定不同的ID?我尝试制作randon编号,但是向下滚动时,第一个隐藏的问题重复了该编号。
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;
}
}