Android显示随机问题

时间:2016-01-22 13:03:39

标签: android

我如何显示随机问题。我查看了其他帖子,但那里的答案没有用。这有3个难度容易中等难度。我想每个难度随机提出问题。感谢

QuizActivity:

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();
    }


}

数据库:

 public class DbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "QUIZ";
    private static final String TABLE_QUIZ = "quiz";
    private static final String KEY_ID = "id";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer";
    private static final String KEY_OPTA = "opta";
    private static final String KEY_OPTB = "optb";
    private static final String KEY_OPTC = "optc";
    private static final String KEY_OPTD = "optd";
    private static final String q_level = "level";
    private static final String QuestionNumber = "q_number";
    private SQLiteDatabase dbase;
    public String question, optionA, optionB, optionC, optionD, answer;

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase = db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUIZ + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
                + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
                + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT, " + KEY_OPTD  +" TEXT, " + q_level + " TEXT, " + QuestionNumber + " INTEGER)";
        db.execSQL(sql);
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','EASY','1')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q2','A1','A2','A3','A4','Answer','EASY','2')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q3','A1','A2','A3','A4','Answer','EASY','3')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q4','A1','A2','A3','A4','Answer','EASY','4')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q5','A1','A2','A3','A4','Answer','EASY','5')");



        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','MEDIUM','1')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','MEDIUM','2')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','MEDIUM','3')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','MEDIUM','4')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','MEDIUM','5')");


        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','HARD','1')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','HARD','2')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','HARD','3')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','HARD','4')");
        db.execSQL("INSERT INTO " + TABLE_QUIZ + "(" + KEY_QUES + ", " + KEY_OPTA + ", " + KEY_OPTB + ", " + KEY_OPTC + ", " + KEY_OPTD + ", " + KEY_ANSWER + ", " + q_level + ", " + QuestionNumber + ")VALUES(" +
                "'Q1','A1','A2','A3','A4','Answer','HARD','5')");
        addQuestions();
        //db.close();
    }

    private void addQuestions() {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUIZ);
        onCreate(db);
    }



    public void getQuestByLevel(String level, int qnum) {
        String selectQuery = "SELECT * FROM " + TABLE_QUIZ + " WHERE " + q_level  +" = '" + level + "' AND "+QuestionNumber+" = "+qnum+"" ;


        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {

                question=cursor.getString(cursor.getColumnIndex(KEY_QUES));
                optionA=cursor.getString(cursor.getColumnIndex(KEY_OPTA));
                optionB=cursor.getString(cursor.getColumnIndex(KEY_OPTB));
                optionC=cursor.getString(cursor.getColumnIndex(KEY_OPTC));
                optionD=cursor.getString(cursor.getColumnIndex(KEY_OPTD));
                answer=cursor.getString(cursor.getColumnIndex(KEY_ANSWER));


            } while (cursor.moveToNext());

        }
    }
}

logcat的:

 01-22 23:23:41.285    8685-8685/com.dreamteam.quiz.project 
E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.dreamteam.quiz.project, PID: 8685
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamteam.quiz.project/com.example.quiz.project.QuizActivity}: android.database.sqlite.SQLiteException: near "ORDER": syntax error (code 1): , while compiling: SELECT * FROM quiz WHERE level = 'EASY' AND q_number = 1 + ORDER BY RANDOM() LIMIT 5
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2319)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5426)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.database.sqlite.SQLiteException: near "ORDER": syntax error (code 1): , while compiling: SELECT * FROM quiz WHERE level = 'EASY' AND q_number = 1 + ORDER BY RANDOM() LIMIT 5
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1112)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:689)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1433)
            at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1372)
            at com.example.quiz.project.DbHelper.getQuestByLevel(DbHelper.java:97)
            at com.example.quiz.project.QuizActivity.onCreateQuestion(QuizActivity.java:60)
            at com.example.quiz.project.QuizActivity.onCreate(QuizActivity.java:45)
            at android.app.Activity.performCreate(Activity.java:5296)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5426)
            at java.lang.reflect.Method.invokeNative(Native Method)

2 个答案:

答案 0 :(得分:0)

使用查询

String selectQuery = "SELECT * FROM " + TABLE_QUIZ + " WHERE " + q_level  +" = '" + level + "' AND "+QuestionNumber+" = "+qnum+" + ORDER BY RANDOM() LIMIT 1"  ;

我认为通过更改查询,您可以参加解决方案。

答案 1 :(得分:0)

将结果存储在Model类中,然后将其随机化。

class Question{
    int id;
    String questionText;
    String[] options;
}


Question[] questions = new Question[cursor.size()];
//put data into questions
Random rnd = new Random();
for(int i = 0; i < question.length; i++){
    //using randomizer swap objects
}

现在选择要显示的TOP 5或10或20个问题,不会有重复,也会是随机顺序