从按钮

时间:2018-02-10 14:05:49

标签: java android button multiple-select

我的应用程序的游戏屏幕。

Game screen of my application.

大家好,我目前正在开发我的测验应用游戏,其主要机制是每个问题中可能有一个或多个可能的正确答案。到目前为止,我只能从给出的选择中选择一个正确答案。

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;
}

布尔值正确用于在数据库中设置正确答案,其值为true设置为1.

数据库中的正确答案,设置为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);

            }
        }
    }
}

如果选择了选项中的答案并且调用了answerButtonClickHandler,则onClick方法会禁用要回答的答案。

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);

}  

上面的代码片段显示了如何将答案导入数据库。

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);
        }

    }
}

我无法想办法如何比较用户选择的答案和我插入数据库的答案。

1 个答案:

答案 0 :(得分:1)

我认为您可以使用CHeckBoxes而不是使用按钮并在某些数组中获取二进制值并将其与您的数据库值进行比较,我认为这可能更简单,更清晰作为UI透视图。