I can't figure out what's wrong with my app. I have a quiz app (just put random questions and answers for now) with all of the questions and answers stored in a SQLite database. When I test the app, it says that the answers are wrong even when they are correct.
The answer is stored as A, B, C, D, and I'm not sure how to make this fit with the real answers.
Here's my check answer code:
private void checkAnswer() {
//TODO: check answer
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
if (currentQuestion.getAnswer().equals(answer.getText())) {
Toast.makeText(getApplicationContext(), R.string.correct_toast, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Chosen answer " + answer.getText().toString() +
" Real answer " + currentQuestion.getAnswer(),
Toast.LENGTH_LONG).show();
}
currentQuestion = quesList.get(mQuestionId);
setQuestionView();
}
It's called in onCreate here:
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer();
}
});
Here's the database helper class:
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 = "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 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("How many houses can I buy?",
"1", "2", "3", "4",
"A");
this.addQuestion(q1, db);
Question q2 = new Question("Which of the following is NOT an option ",
"Apartments", "Houses", "Duplexes", "Rentals",
"D");
this.addQuestion(q2, db);
Question q3 = new Question("Which of the following is the fastest way of buying",
"Credit card", "Loan", "Cash", "Handshakes",
"C");
this.addQuestion(q3, db);
Question q4 = new Question("Should you use an agent or the internet",
"Agent", "Internet", "Neither", "Both",
"A");
this.addQuestion(q4, db);
Question q5 = new Question("Which of the following is not an available language",
"English", "Italian", "Spanish", "Hungarian",
"C");
this.addQuestion(q5, db);
Question q6 = new Question("Question 6",
"Answer 1", "Answer 2 - correct", "Answer 3", "Answer 4",
"B");
this.addQuestion(q6, 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) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQuestion());
values.put(KEY_ANSWER, quest.getAnswer());
values.put(KEY_OPTA, quest.getOptionA());
values.put(KEY_OPTB, quest.getOptionB());
values.put(KEY_OPTC, quest.getOptionC());
values.put(KEY_OPTD, quest.getOptionD());
// Inserting Row
db.insert(TABLE_QUEST, null, values);
}
Thanks for the help!