在Android App测验中随机化和循环问题

时间:2016-08-30 14:02:31

标签: android sqlite

我感到困惑。我正在开发一个测验。我一直试图随机化问题。我试图在Cursor和Rawquery中随机化无济于事。

与此同时,我希望应用程序在最后一个问题之后循环播放问题。

这是我的Question.java(这是我的活动)

public class Question extends Activity {


    public static final String TAG = Question.class.getSimpleName();
    private TextView mTimer;
    private Button mTimerButton;
    private CountDownTimer mCountDownTimer;
    private TextView mQuestionTextView;
    private Button mNextQuestion;
    private Button mHowtoplay2;
    List<QuestionFaci> quesList;
    int qid = 0;

    QuestionFaci currentQ;

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState( outState );


    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState( savedInstanceState );


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_question );

        mHowtoplay2 = (Button) findViewById( R.id.howtoplaybutton );
        mHowtoplay2.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startHowtoplay();
            }
        } );


        QuizHelper db = new QuizHelper( this );
        quesList = db.getAllQuestions();
        currentQ = quesList.get(qid);

        mQuestionTextView = (TextView) findViewById( R.id.questionView  );
        mNextQuestion = (Button)findViewById( R.id.nextbutton );

        View.OnClickListener listener = new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                currentQ=quesList.get(qid);
                setQuestionView();
            }
        };

        mNextQuestion.setOnClickListener( listener );
        Log.d(TAG, "We're logging from the onCreate() method");


    private void startHowtoplay(){
        Intent intent = new Intent( this, Howtoplay.class );
        startActivity( intent );
    }
    private void setQuestionView(){
        mQuestionTextView.setText( currentQ.getQUESTION() );
        qid++;
    }
}

这是我的QuizHelper

public class QuizHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "creativequestion";
    private static final String TABLE_QUEST = "quest";
    private static final String KEY_ID = "qid";
    private static final String KEY_QUEST = "question";

    private SQLiteDatabase dbase;

    public QuizHelper(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_QUEST + " ( " + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUEST + " TEXT)";
        db.execSQL( sql );
        addQuestion();
    }

    private void addQuestion() {

        QuestionFaci q1 = new QuestionFaci( "OTHER USES: Name other uses of a Hammer. \n\n Example: Stir a soup." );
        this.addQuestion(q1);
        QuestionFaci q2 = new QuestionFaci( "RHYMES: Words that rhymes with Rice. \n\n Example: Ice" );
        this.addQuestion(q2);
        QuestionFaci q3 = new QuestionFaci( "WITH: I can cook eggs with... \n\n Example: A Piece of Plywood" );
        this.addQuestion(q3);
        QuestionFaci q4 = new QuestionFaci( "WITHOUT: I can wash my clothes without... \n\n Example: My Aunt" );
        this.addQuestion(q4);
        QuestionFaci q5 = new QuestionFaci( "I WILL: If I was Bill Gates, I will... \n\n Example: Buy a spaceship" );
        this.addQuestion(q5);
        QuestionFaci q6 = new QuestionFaci( "CREATE A MOVIE TITLE: A NIGHT \n\n Example: To Remember" );
        this.addQuestion(q6);
        QuestionFaci q7 = new QuestionFaci( "OTHER NAMES: Other names of a cow \n\n Example: Milk giver" );
        this.addQuestion(q7);
    }

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

    }
    private void addQuestion(QuestionFaci quest) {
        ContentValues values = new ContentValues(  );
        values.put(KEY_QUEST, quest.getQUESTION());
        dbase.insert( TABLE_QUEST, null, values );
    }


    public List<QuestionFaci> getAllQuestions(){
        List<QuestionFaci> quesList = new ArrayList<QuestionFaci>(  );
        String selectQuery = "SELECT * FROM " + TABLE_QUEST;
        dbase = this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery( selectQuery, null );
        if (cursor.moveToFirst()){
            do{
                QuestionFaci quest = new QuestionFaci(  );
                quest.setID( cursor.getInt( 0 ) );
                quest.setQUESTION( cursor.getString( 1 ) );

                quesList.add(quest);
                } while (cursor.moveToNext());
         }

        return quesList;

    }
}

这是我的QuestionFaci,我的getter和setter

public class QuestionFaci extends Activity {

    private int ID;
    private String QUESTION;

    public QuestionFaci(){

        ID=0;
        QUESTION= "";

    }

    public QuestionFaci (String qUESTION){
        QUESTION = qUESTION;
    }

    public int getID(){
        return ID;
    }

    public String getQUESTION(){
        return QUESTION;
    }

    public void setID(int id){
        ID = id;
    }

    public void setQUESTION(String qUESTION){
        QUESTION = qUESTION;
    }
}

1 个答案:

答案 0 :(得分:1)

替换为:

private void setQuestionView(){
    Random r = new Random();
    int Low = 0;
    int High = quesList.size();
    int Result = r.nextInt(High-Low) + Low; // 0 - questions size() -> select random
    qid = Result; 
    mQuestionTextView.setText( currentQ.getQUESTION() ); // set Question

}

如果您希望以前的问题不再出现,则必须以不同的方式进行。

P.S:这不会使您的问题在您的数据库中随机化。它随机化了qid,因此它会从列表中随机提出一个问题。您可以随机化从数据库中获得的列表,如果您使用HashMap,那么您将知道之前是否出现过问题。