Reputation: 31
I am currently have a quiz app which consists of a question & four buttons which is options. At the end of quiz, the app displays the correct answers for all the questions. now I want to display correct answer and player selected answer at the end of quiz. Here's the code that I currently working to display the answers.
public static String getAnswers(List<Question> questions) {
int question = 1;
StringBuffer sb = new StringBuffer();
for (Question q : questions){
sb.append("Q").append(question).append(") ").append(q.getQuestion()).append("? \n");
sb.append("Answer: ").append(q.getAnswer()).append("\n\n");
question ++;
}
return sb.toString();}
and my QuesActivity is:
private void setQuestions() {
//set the question text from current question
String question = Utility.capitalise(currentQ.getQuestion()) + "?";
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
btn1 = (Button) findViewById(R.id.button1);
btn1.setText(Utility.capitalise(answers.get(0)));
btn1.setOnClickListener(this);
btn2 = (Button) findViewById(R.id.button2);
btn2.setText(Utility.capitalise(answers.get(1)));
btn2.setOnClickListener(this);
btn3 = (Button) findViewById(R.id.button3);
btn3.setText(Utility.capitalise(answers.get(2)));
btn3.setOnClickListener(this);
btn4 = (Button) findViewById(R.id.button4);
btn4.setText(Utility.capitalise(answers.get(3)));
btn4.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
if (!checkAnswer()) return;
if (currentGame.isGameOver()){
Log.d("Questions", "End of game! lets add up the scores..");
Log.d("Questions", "Questions Correct: " + currentGame.getRight());
Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
Intent i = new Intent(this, EndgameActivity.class);
startActivity(i);
finish();
}
else{
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
}
private boolean checkAnswer() {
String answer = getSelectedAnswer();
Log.d("yourans", currentQ.getAnswer()+" "+answer);
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
Log.d("Questions", "Correct Answer!");
currentGame.incrementRightAnswers();
}
else{
Log.d("Questions", "Incorrect Answer!");
currentGame.incrementWrongAnswers();
}
return true;
}
private String getSelectedAnswer() {
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
Button b3 = (Button)findViewById(R.id.button3);
Button b4 = (Button)findViewById(R.id.button4);
if (b1.isPressed())
{
return b1.getText().toString();
}
if (b2.isPressed())
{
return b2.getText().toString();
}
if (b3.isPressed())
{
return b3.getText().toString();
}
if (b4.isPressed())
{
return b4.getText().toString();
}
return null;
}}
Upvotes: 2
Views: 629
Reputation: 307
Add another property in Class Question named, userAnswer. One field will be for correct answer and one will be for user answer. There will be another propety isCorrect. You have a class named Question. It would be like that, I am ignoring the syntax.
class Question {
String mQuestion;
String mOption1;
String mOption2;
String mOption3;
String mOption4;
String mCorrectAnswer;
String mUserAnswer;
String mStatusOfQuestion (Asked/Not Asked Yet)
}
You will set your questions in a way you are doing right now. But set the class objects as well like and put them in an arrayList.
ArrayList<Question> mQuestionsList = new ArrayList<Question>
setQuestion() {
Question mQuestion = new Question();
String question = Utility.capitalise(currentQ.getQuestion()) + "?";
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
mQuestion.setQuestion(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
btn1 = (Button) findViewById(R.id.button1);
btn1.setText(Utility.capitalise(answers.get(0)));
mQuestion.setOption1(answers.get(0));
btn1.setOnClickListener(this);
btn2 = (Button) findViewById(R.id.button2);
btn2.setText(Utility.capitalise(answers.get(1)));
mQuestion.setOption2(answers.get(1));
btn2.setOnClickListener(this);
btn3 = (Button) findViewById(R.id.button3);
btn3.setText(Utility.capitalise(answers.get(2)));
btn3.setOnClickListener(this);
mQuestion.setOption3(answers.get(2));
btn4 = (Button) findViewById(R.id.button4);
btn4.setText(Utility.capitalise(answers.get(3)));
btn4.setOnClickListener(this);
mQuestion.setOption4(answers.get(3));
mQuestion.setCorrectAnswer(corectAnswer);
}
When user taps on an option set the value of object of question like
onUserclick(int position 2) {
mQuestionsList.get(position).setUserAnswer(tappedOption);
if(tappedOption == mQuestionsList.get(position).getCorrectAnswer())
isCorrect = true;
else
isCorrect = false;
}
Upvotes: 2