2016-09-19 41 views
0

我想每次加载不同的问题。我尝试了下面的代码,但它只是随机更改第一个问题。所有其他问题都按顺序排列。从数据库加载随机Qid(问题)?

QuizHelper db = new QuizHelper(this); // my question bank class 
     quesList = db.getAllQuestions(); // this will fetch all quetonall questions 
     Random random = new Random(); currentQ = quesList.get(random.nextInt(quesList.size())); 

在此处输入完整代码。

public class QuestionActivity extends Activity { 
    List<Question> quesList; 

    int score = 0; 
    int qid = 0;  

    Question currentQ; 
    TextView txtQuestion, times, scored; 
    Button button1, button2, button3;  

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     QuizHelper db = new QuizHelper(this); // my question bank class 
     quesList = db.getAllQuestions(); // this will fetch all quetonall questions 
     Random random = new Random(); currentQ = quesList.get(random.nextInt(quesList.size())); 
     // currentQ = quesList.get(qid); // the current question 

     txtQuestion = (TextView) findViewById(R.id.txtQuestion); 
     // the textview in which the question will be displayed 

     // the three buttons, 
     // the idea is to set the text of three buttons with the options from question bank 
     button1 = (Button) findViewById(R.id.button1); 
     button2 = (Button) findViewById(R.id.button2); 
     button3 = (Button) findViewById(R.id.button3); 

     // the textview in which score will be displayed 
     scored = (TextView) findViewById(R.id.score); 

     // the timer 
     times = (TextView) findViewById(R.id.timers);  

     // method which will set the things up for our game 
     setQuestionView(); 
     times.setText("00:02:00"); 

     // A timer of 60 seconds to play for, with an interval of 1 second (1000 milliseconds) 
     CounterClass timer = new CounterClass(80000, 1000); 
     timer.start(); 

     // button click listeners 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       // passing the button text to other method 
       // to check whether the anser is correct or not 
       // same for all three buttons 
       getAnswer(button1.getText().toString()); 
      } 
     }); 

     button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       getAnswer(button2.getText().toString()); 
      } 
     }); 

     button3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       getAnswer(button3.getText().toString()); 
      } 
     }); 
    } 

    public void getAnswer(String AnswerString) { 
     if (currentQ.getANSWER().equals(AnswerString)) { 

      // if conditions matches increase the int (score) by 1 
      // and set the text of the score view 
      score++; 
      scored.setText("Score : " + score); 
     } else { 

      // if unlucky start activity and finish the game 

      Intent intent = new Intent(QuestionActivity.this, 
        ResultActivity.class); 

      // passing the int value 
      Bundle b = new Bundle(); 
      b.putInt("score", score); // Your score 
      intent.putExtras(b); // Put your score to your next 
      startActivity(intent); 
      finish(); 
     } 
     if (qid < 25) { 

      // if questions are not over then do this 
      currentQ = quesList.get(qid); 
      setQuestionView(); 
     } else { 

      // if over do this 
      Intent intent = new Intent(QuestionActivity.this, 
        ResultActivity.class); 
      Bundle b = new Bundle(); 
      b.putInt("score", score); // Your score 
      intent.putExtras(b); // Put your score to your next 
      startActivity(intent); 
      finish(); 
     }  
    }  

    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @SuppressLint("NewApi") 
    public class CounterClass extends CountDownTimer { 

     public CounterClass(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public void onFinish() { 
      times.setText("Time is up"); 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      // TODO Auto-generated method stub 

      long millis = millisUntilFinished; 
      String hms = String.format(
        "%02d:%02d:%02d", 
        TimeUnit.MILLISECONDS.toHours(millis), 
        TimeUnit.MILLISECONDS.toMinutes(millis) 
          - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS 
          .toHours(millis)), 
        TimeUnit.MILLISECONDS.toSeconds(millis) 
          - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS 
          .toMinutes(millis))); 
      System.out.println(hms); 
      times.setText(hms); 
     } 
    } 

    private void setQuestionView() { 

     // the method which will put all things together 
     txtQuestion.setText(currentQ.getQUESTION()); 
     button1.setText(currentQ.getOPTA()); 
     button2.setText(currentQ.getOPTB()); 
     button3.setText(currentQ.getOPTC()); 

     qid++; 
    }  
} 
+0

'//如果问题不能在那么做currentQ = quesList.get(QID);' 什么不清楚的吗? – chrylis

回答

1

这是由于由1只增加QID并在您的getAnswer使用

currentQ = quesList.get(qid); 

()。使你的随机全球然后尝试改变当前Q到

currentQ = quesList.get(random.nextInt(quesList.size())); 

here是如何避免重复随机。

[编辑]

好,使其更简单这样做是为了避免重复。做一个全球INT位置

position = random.nextInt(quesList.size()) 
currentQ = quesList.get(position); 

然后在你setQuestion

quesList.remove(position); 

你去那里结束,问题永远不会重复。希望有所帮助。

+0

我试过这个,但只有第一个问题是随机的。从第二个问题开始,它总是按顺序排列。 – user3004860

+0

你改变了'currentQ = quesList.get(qid);''getAnswer(String AnswerString)'内部并仍然得到相同的结果? –

+0

如果我在oncreate()中改变currentq,它只能提供第一个问题。 如果currentq在getanswer()中更改,它将起作用,但会出现大量重复问题。我检查了您的参考,但无法解决重复。 – user3004860

0
currentQ = quesList.get(random.nextInt(quesList.size())); 

使用这种insted的QID的

+0

请编辑更多信息。仅限代码和“尝试这个”的答案是不鼓励的,因为它们不包含可搜索的内容,也不解释为什么有人应该“尝试这个”。 – abarisone