从按钮中选择多个正确答案 [英] Selecting multiple correct answers from buttons

查看:62
本文介绍了从按钮中选择多个正确答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

The boolean correct is used for setting the correct answers in the database which has a value of true set to 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);

            }
        }
    }
}

如果已经选择了一个答案,则onClick方法将禁用剩下的答案,并调用answerButtonClickHandler.

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.

推荐答案

我认为您可以使用CHeckBoxes而不是使用按钮,并在某个数组中获取二进制值并将其与数据库值进行比较,我认为这可能会更容易并且会更多作为UI透视图进行清洁.

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.

这篇关于从按钮中选择多个正确答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆