问题
Game screen of my application.
Hello everyone, I am currently in the middle of developing my quiz app game, and its main mechanics is it's possible that there is one or more possible correct answers in each question. So far, I can only select one correct answer out of the choices given.
private Answer getAnswerFromCursor (Cursor cursor){
Answer answer = new Answer();
answer.setAnswerId(cursor.getInt(FIELD_ID_ID));
answer.setQuestionId(cursor.getInt(FIELD_ID_QUESTIONID));
answer.setText(cursor.getString(FIELD_ID_TEXT));
boolean correct = (cursor.getInt(FIELD_ID_CORRECT)==1);
answer.setCorrect(correct);
return answer;
}
The boolean correct is used for setting the correct answers in the database which has a value of true set to 1.
The correct answers in the database, set to 1.
private void displayQuestion(Question question) {
if (question != null) {
answers = answerData.getAnswersbyQuestionId(question.getQuestionId());
List<Integer> myAnswersIndexList = new ArrayList<Integer>();
for (int answerId : answers.keySet()) {
myAnswersIndexList.add(answerId);
}
Collections.shuffle(myAnswersIndexList);
answersFrame.removeAllViews();
questionText.setText(question.getText());
if(answers!=null){
answersButtonsList = new ArrayList<Button>();
Answer answer = null;
for(int answerId : myAnswersIndexList) {
Log.i("Captchabuster", "Adding question with Id " + answerId);
answer = answers.get(answerId);
Button answerButton = new Button(this);
answerButton.setId(answer.getAnswerId());
answerButton.setText(answer.getText());
//handle the button-clicker
answerButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//timer.cancel();
disableAnswerButtons();
answerButtonClickHandler(v);
}
});
answersButtonsList.add(answerButton);
answersFrame.addView(answerButton, answerButtonLayout);
}
}
}
}
The onClick method disables the answers left to be answered if an answer out of the choices has been selected and calls the answerButtonClickHandler.
else if(questionId == 8){
importAnswersData(db, questionId, recordData[4], false);
importAnswersData(db, questionId, recordData[6], true);
importAnswersData(db, questionId, recordData[5], false);
importAnswersData(db, questionId, recordData[7], true);
importAnswersData(db, questionId, recordData[8], true);
importAnswersData(db, questionId, recordData[9], true);
}
The code snippet above shows how to import the answers to the database.
public void answerButtonClickHandler(View v)
{
Answer answer = answers.get(v.getId());
if (answer != null) {
questionAnswered++;
if (answer.isCorrect()) {
correctlyAnswered++;
}
/* add template here
v.setBackgroundResource(R.drawable.answer_button_correct);
else{
v.setBackgroundResource(R.drawable.answer_button_wrong);
}*/
questionDescriptionText.setText(question.getDescription());
questionDescriptionText.setVisibility(View.VISIBLE);
if (nextQuestionIndex < questionsIndexList.size()) {
nextBtn.setVisibility(View.VISIBLE);
} else{
completeBtn.setVisibility(View.VISIBLE);
}
}
}
I can't think of a way on how will I compare what answers the user has selected to the answers I inserted into the database.
回答1:
I think you can use CHeckBoxes instead of using buttons and take binary values in some array and compare that to your database values, I think that might be easier and more clean as an UI perspective.
来源:https://stackoverflow.com/questions/48721615/selecting-multiple-correct-answers-from-buttons