检视正确的答案

时间:2012-04-10 09:46:33

标签: android database

我正在开发一个问题游戏的应用程序。我设法将每个问题的答案显示为每次不同的按钮,但我无法检查正确的答案。我的方法是:我在数据库中创建了一个额外的列,其中我指出了正确答案为(1,2,3或4)的列。我使用此代码在不同的按钮中显示答案。

cur = dbHelper.getRandomQuestion();
    String corrans = cur.getString(cur.getColumnIndex("CorrectAnswer"));
    a = Integer.parseInt(corrans);
    String question = cur.getString(cur.getColumnIndex("QUESTIONS"));
    String answer0 = cur.getString(cur.getColumnIndex("ANSWER1"));
    String answer1 = cur.getString(cur.getColumnIndex("ANSWER2"));
    String answer2 = cur.getString(cur.getColumnIndex("ANSWER3"));
    String answer3 = cur.getString(cur.getColumnIndex("ANSWER4"));
    txtQuest.setText(question);

    ArrayList<String> lstAnswers = new ArrayList<String>();
    lstAnswers.add(answer0);
    lstAnswers.add(answer1);
    lstAnswers.add(answer2);
    lstAnswers.add(answer3);

     score.setText("Your score is " + b +","+ a);

    Random random = new Random();

    int[] textViews = new int[] { R.id.button1, R.id.button2, R.id.button3, R.id.button4 };
    int textViewIndex = 0;

    while (lstAnswers.size() > 0)   {
    int index = random.nextInt(lstAnswers.size());
    if(a == index){ b = index;}
    else{}
    String randomAnswer = lstAnswers.remove(index);
    ((TextView)findViewById(textViews[textViewIndex])).setText(randomAnswer);
    ++textViewIndex;
                                    }

对每个按钮调用以比较a和b的值,然后相应地采取行动。但它似乎没有用。我理解为什么,但我无法弄清楚。任何帮助都可以。

3 个答案:

答案 0 :(得分:2)

使用Collections.shuffle(list)随机播放您的答案数组,然后显示答案    将标记设置为“正确”到您的按钮,其中anwer正确,然后比较tagvalue和disply对或错

编辑:下面的内容只是一个大纲

 Here im displaying answers in buttons....

    List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0,1,2,3));
        Collections.shuffle(intList);

       Log.d("ERR","List after shuffling: " + intList);  
       // below answers will be assiagned randomly to buttons...
        btn_cmpTagline[intList.get(0)].setText(answr1);
        btn_cmpTagline[intList.get(0)].setTag("right");
        btn_cmpTagline[intList.get(1)].setText(answr2);
        btn_cmpTagline[intList.get(1)].setTag("wrong");
        btn_cmpTagline[intList.get(2)].setText(answr3);
        btn_cmpTagline[intList.get(2)].setTag("wrong");
        btn_cmpTagline[intList.get(3)].setText(answr4);
        btn_cmpTagline[intList.get(3)].setTag("wrong");

//点击

   @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn_tag1:
            Log.d("ERR", v.getTag().toString());
            if(v.getTag().toString().equalsIgnoreCase("right")){
                //this button has right answer .. do anything 
            }

            break;
        case R.id.btn_tag2:
            Log.d("ERR", v.getTag().toString());
            if(v.getTag().toString().equalsIgnoreCase("right")){
                //this button has right answer .. do anything 
            }

            break;
        case R.id.btn_tag3:
            Log.d("ERR", v.getTag().toString());
            if(v.getTag().toString().equalsIgnoreCase("right")){
                //this button has right answer .. do anything 
            }

                    --
                    --
      }

答案 1 :(得分:0)

当您从列表中删除项目时,正确答案的索引将会更改,因此您必须更新正确答案所在的位置。

答案 2 :(得分:0)

您可以获得正确答案的值而不是索引。然后,当您添加按钮的答案时,您可以检查它是否与正确答案相同并保留新索引。