我有一個測驗應用程序,它顯示了幾個來自SQLite Db的問題,它有50個問題,但它必須在遊戲部分顯示5個問題,並將隨機化這些問題。Android ::我如何隨機化我的測驗問題?
我厭倦了爲解決多次,但沒有:/
在源代碼中QID是分貝。基於顯示問題並找到答案的ID主鍵。
這裏的活動:
package com.example.quiz;
import java.util.List;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.DatabaseUtils;
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;
public class QuizActivity extends Activity {
private static final DbHelper remote = null;
List <Question> quesList;
int score = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;
int qid;
int qNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
DbHelper db = new DbHelper(this);
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
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);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
Log.d("score", "Your score"+score);
}
if(qNumber < 5){
currentQ = quesList.get(qid);
setQuestionView();
}
else
{
Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Pontunk
intent.putExtras(b); //Pont tovább vivése a következő kérdéshez
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
Random rand = new Random();
for(int i=0; i<5;i++){
int r1 = rand.nextInt(50) + 1;
qid = r1;
}
qNumber++;
}
}
當qNumber的值超過5時,重定向到ResultsActivity。 我測試了它,它的工作,顯示了隨機問題,但有時程序崩潰了。 我錯在哪裏,爲什麼不正常的工作隨機化?
感謝您的幫助,並對不起我的英語知識!
爲什麼不選擇一個隨機的非經常性指數,只是拉該索引的問題嗎? – Adam
好主意,謝謝! – Brhama