I am doing an App of test questions, in which 30 questions should be left with their possible markable answers in the same layout. My doubts are when I have to create a database. I have created the res -> raw folder where I keep the questions and answers as follows:
pregunta1;opcionBuena;opcionmala1;opcionmala2;img1
pregunta2;opcionBuena;opcionmala1;opcionmala2;img2
etc..
Now following tutorials and examples I have created the DBContract classes:
public class DBContract {
public static abstract class TestEntry implements BaseColumns{
public static final String TABLE_NAME ="bdTest";
public static final String PREGUNTA = "pregunta";
public static final String CORRECT_ANSWER = "correctAnswer";
public static final String DISTRACTOR1 = "distractor1";
public static final String DISTRACTOR2 = "distractor2";
public static final String IMAGEN = "imagen";
}
}
And DBHelper:
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "BBDD.db";
public static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE " + DBContract.TestEntry.TABLE_NAME+ "("
+ DBContract.TestEntry._ID + " INTEGER PRIMARY KEY"
+ DBContract.TestEntry.PREGUNTA + "TEXT NOT NULL, "
+ DBContract.TestEntry.CORRECT_ANSWER + "TEXT NOT NULL, "
+ DBContract.TestEntry.DISTRACTOR1 + "TEXT NOT NULL, "
+ DBContract.TestEntry.DISTRACTOR2 + "TEXT NOT NULL, "
+ DBContract.TestEntry.IMAGEN + "TEXT )");
lecturaBaseDatos(sqLiteDatabase);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private ArrayList<Question> lecturaBaseDatos(SQLiteDatabase s) {
ArrayList<Question> test = new ArrayList<Question>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + DBContract.TestEntry.TABLE_NAME,null);
if(c.moveToFirst()){
do{
test.add(new Question(
c.getString(1),
c.getString(2),
c.getString(3),
c.getString(4),
c.getString(5)));
} while (c.moveToNext());
}
db.close();
return test;
}
}
I also have a Test class () that is a list of the Question () class which forms the complete question with its answers.
This is the first time that I have made a database and I know that it is badly implemented but I can not find a way to do it well, especially the readingBaseData method that I have adapted it with respect to a found example.
I would also like to know how I should call showing the test list in the Activity that shows the tests.
Thank you.