2016-01-21 90 views
-1

我如何可以隨機將與RadioButton顯示的問題。我看了其他帖子,但答案不適合我。的Android測驗隨機出題

import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.List; 

public class QuizActivity extends Activity { 
    int score = 0; 
    int qnum = 1; 
    TextView txtQuestion; 
    RadioButton rda, rdb, rdc, rdd; 
    Button butNext; 
    RadioGroup rdgrp; 

    String corAnswer = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     setContentView(R.layout.activity_quiz); 
     DbHelper db = new DbHelper(this); 
     rdgrp = (RadioGroup) findViewById(R.id.questionAndAnswers); 
     txtQuestion = (TextView) findViewById(R.id.textView1); 
     rda = (RadioButton) findViewById(R.id.radio0); 
     rdb = (RadioButton) findViewById(R.id.radio1); 
     rdc = (RadioButton) findViewById(R.id.radio2); 
     rdd = (RadioButton) findViewById(R.id.radio3); 
     butNext = (Button) findViewById(R.id.button1); 


     corAnswer = ""; 
     onCreateQuestion(); 
    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_quiz, menu); 
     return true; 
    } 


    public void onCreateQuestion() { 
     String level = getIntent().getExtras().getString("level"); 
     DbHelper db = new DbHelper(this); 
     db.getQuestByLevel(level, qnum); 

     txtQuestion.setText(db.question); 
     rda.setText(db.optionA); 
     rdb.setText(db.optionB); 
     rdc.setText(db.optionC); 
     rdd.setText(db.optionD); 
     corAnswer = db.answer; 

     qnum++; 

    } 

    public void onClickNext(View view) { 
     String level = getIntent().getExtras().getString("level"); 
     DbHelper db = new DbHelper(this); 
     db.getQuestByLevel(level, qnum); 

     RadioGroup grp = (RadioGroup) findViewById(R.id.questionAndAnswers); 
     RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId()); 
     if (answer == null) 
     { 
      Toast.makeText(QuizActivity.this, "select an answer please", Toast.LENGTH_SHORT).show(); 


      return; 
     } 
     if (corAnswer!= null && corAnswer.equalsIgnoreCase((String) answer.getText())) 
     { 
      score++; 
      Log.d("answer", "Your score" + score); 
     } 
     if (qnum <= 5) { 


     } else { 
      Intent intent = new Intent(QuizActivity.this, ResultActivity.class); 
      Bundle b = new Bundle(); 
      b.putInt("score", score); 
      intent.putExtras(b); 
      startActivity(intent); 
      finish(); 
     } 

     txtQuestion.setText(db.question); 
     rda.setText(db.optionA); 
     rdb.setText(db.optionB); 
     rdc.setText(db.optionC); 
     rdd.setText(db.optionD); 
     corAnswer = db.answer; 
     qnum++; 
     rdgrp.clearCheck(); 
    } 


} 
+1

什麼是您的具體問題?你看過哪些帖子,哪些不起作用? – PKeno

+0

這裏沒有問題。我只需要證明隨機抽題 – Cyril

+0

@Cyril如果沒有問題,那麼毫無疑問 –

回答

2

將您的問題以Array然後洗牌這個數組:

List<String> myArray = new ArrayList<String>(Arrays.asList(questionArray)); 
Collections.shuffle(myArray); 

或者直接用您的SQL查詢,你可以使用:

... ORDER BY RANDOM()... 

正如你可以看到這裏,我已經有了一個簡單的表格[cyril_test]這樣的:

enter image description here

然後,我與執行的順序查詢通過隨機():

enter image description here

,其結果是:

enter image description here

+0

我的問題是在數據庫中。有沒有什麼辦法可以讓他們洗牌而不會讓他們進入數組?對不起,我只是在編碼 – Cyril

+0

@Cyril,我編輯了我的文章。 –

+0

好的建議。沒有跨過我的腦海關於SQL查詢。 +1 – Rohit5k2