mercredi 2 mars 2016

How to randomize questions without repetition in java for android studio?

I'm developing a simple quiz in my android studio. I'm using a database but I would like to randomize the questions every time the user clicks on the quiz button. I have created 5 sample questions so far. This is what I have so far.

public class DbHelperMarketing extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private static final String KEY_OPTD= "optd"; //option d
private SQLiteDatabase dbase;

public DbHelperMarketing(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_QUES
            + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
            +KEY_OPTB +" TEXT, "+KEY_OPTC +" TEXT, "+KEY_OPTD +" TEXT)";
    db.execSQL(sql);
    addQuestions();

}
private void addQuestions()
{
    Question q1=new Question("Which of the following is usually the last step in the buying process:","Placing the order", "Selecting Delivery Method", "Deciding on Goods", "Negotiating the Price", "B");
    this.addQuestion(q1);
    Question q2=new Question("Customers who pay cash for goods and services often earn", "Coupons", "Receipts", "Discounts", "Warranties", "C");
    this.addQuestion(q2);
    Question q3=new Question("Which of the following is an action that a buyer should take in order to create a good working relationship with a vendor:",
            "Sharing competitive information among vendors", "Obtaining the vendor's agreement to cancel orders","Keeping shipments that contain more goods than ordered",
            "Failing to report shipments that contain faulty products", "B");
    this.addQuestion(q3);
    Question q4=new Question("The information about profit in a business's profit-and-loss statement helps to establish the __________ of the business's stock.","Style", "Goals", "Value", "Brand", "C");
    this.addQuestion(q4);
    Question q5=new Question("Why is it important for a business to budget a certain amount of money to savings?","To satisfy customers ","To avoid financial crisis","To pay stockholders",
            "To prevent governmental control", "B");
    this.addQuestion(q5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
    // Create tables again
    onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {

    ContentValues values = new ContentValues();
    values.put(KEY_QUES, quest.getQUESTION());
    values.put(KEY_ANSWER, quest.getANSWER());
    values.put(KEY_OPTA, quest.getOPTA());
    values.put(KEY_OPTB, quest.getOPTB());
    values.put(KEY_OPTC, quest.getOPTC());
    values.put(KEY_OPTD, quest.getOPTD());
    // Inserting Row
    dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase=this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Question quest = new Question();
            quest.setID(cursor.getInt(0));
            quest.setQUESTION(cursor.getString(1));
            quest.setANSWER(cursor.getString(2));
            quest.setOPTA(cursor.getString(3));
            quest.setOPTB(cursor.getString(4));
            quest.setOPTC(cursor.getString(5));
            quest.setOPTD(cursor.getString(6));
            quesList.add(quest);
        } while (cursor.moveToNext());
    }
    // return quest list
    return quesList;
}

Any help would be appreciated. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire