2016-01-21 65 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