2016-01-22 73 views
1

我如何顯示隨機問題。我看了其他帖子,但那裏的答案沒有工作。這有三難難度。我想根據難度隨機提出問題。感謝Android秀隨機問題

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) 
+1

您的查詢有語法錯誤:刪除+(PLUS)令前BY並且還要確保查詢必須以;(分號) – kishorepatel

+0

@kishorepatel我試過String selectQuery =「SELECT * FROM」+ TABLE_QUIZ +「WHERE」+ q_level +「='」+ level +「'AND」+ QuestionNumber +「 =「+ qnum +」ORDER BY RANDOM()LIMIT 5「;但它沒有給我隨機問題 – Anon

+0

對於LEVEL 1和QuestionNumber = 1,只有一個元組,因此在結果中只有一行。顯然一行不能有任何隨機性。 – kishorepatel

回答

0

使用查詢:

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

我想通過改變查詢你可以參加解決方案。

+0

我試過了代碼,但應用程序崩潰 – Anon

+0

對不起,它將是RANDOM()。請檢查並讓我知道,如果應用程序仍然崩潰 –

+0

該應用程序仍然崩潰 – Anon

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的問題,也將是隨機的順序

+0

logcat。我不能在那裏隨機化嗎? – Anon

+0

看看Sumit Chakroborty的解決方案是否可行。使用random()for sqlite和LIMIT 5; (或者你想要的數量),我不知道它的速度。也看到這個喜歡寫作查詢http://blog.rodolfocarvalho.net/2012/05/how-to-select-random-rows-from-sqlite.html希望可以幫助 – kishorepatel

+0

我試過這段代碼: String selectQuery =「 SELECT * FROM「+ TABLE_QUIZ +」WHERE「+ q_level +」='「+ level +」'AND「+ QuestionNumber +」=「+ qnum +」+ ORDER BY RANDOM()LIMIT 5「; 但仍然沒有給出隨機問題。 – Anon