将测验问题分成不同的组

时间:2017-12-11 14:13:23

标签: java android

我经常搜索这个问题,但似乎无法找到答案。我的测验应用程序运行良好,但我意识到一次50个问题太多了。

现在,我的应用程序通过人们点击“开始”按钮进行测试。在测验之后,他们出错的所有问题都会显示正确的答案。但是,我想要做的是将这些问题分成10个问题。因此,选择“问题集1”按钮,“问题集2”按钮等。然后,我仍然希望显示正确的答案。这个问题集中包含的问题。

我实际上不确定如何分开问题并使我的逻辑仍然有用。下面我已经包含了数据库助手类。我已经豁免了大部分问题以缩短它。非常感谢你!

public class DbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "realEstateQuiz";
    // tasks table name
    private static final String TABLE_QUEST = "quest";
    // tasks Table Columns names
    private static final String KEY_ID = "questionId";
    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 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_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(db);
        // db.close();
    }

    private void addQuestions(SQLiteDatabase db) {
        Question q1 = new Question(
                "When a real estate professional exaggerates " + "about a parcel of property, they are likely:",
                "Misrepresenting the property", "Guilty of fraud", "Puffing", "All of the above", "Puffing");
        this.addQuestion(q1, db);

    }

    @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, SQLiteDatabase db) {
        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
        db.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.setQuestionId(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
        cursor.close();
        dbase.close();
        return quesList;
    }

    public int rowcount() {
        int row = 0;
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        row = cursor.getCount();

        cursor.close();
        return row;
    }
}

1 个答案:

答案 0 :(得分:0)

在我看来,您的问题对象和相应的表应该包含一个附加字段,用于指示它们属于哪个问题集。然后,您的DbHelper类应该可以通过set查询Questions。或者您也可以按照java代码中的设置对所有问题进行排序,但我更愿意将其留给数据库。

但是,如果您希望将所有问题分成10组,您可以尝试以下内容:

List<Set<Question>> questionSets = new ArrayList<Set<Question>>();
Set<Question> questionSet = new HashSet<Question>();
questionSets.add(questionSet);

int counter = 0;
for(Question question : dbHelper.getAllQuestions()){
  if(++counter > 10){
     counter = 0;
     questionSet = new HashSet<Question>();
     questionSets.add(questionSet);            
  }

  questionSet.add(question);
}

这将收集10个问题到一个集合中,并将它们添加到问题集列表中。然后,您可以从该列表中访问每个问题集。