1

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!

Ang
  • 83
  • 1
  • 3
  • 11

1 Answers1

3

You need to apply toString on your EditText/TextViewto fetch the text otherwise you are just comparing your answer with CharSequence (using getText) reference which will likely to always give you false

if (currentQuestion.getAnswer().equals(answer.getText().toString())) 
//                                                     ^^^^^^^^^^^

Note: RadioButton is descendant of TextView so hence the getText is coming from TextView returning the CharSequence

Update : for simplicity store your questions with answers instead of options

Question q2 = new Question("Which of the following is NOT an option ",
            "Apartments", "Houses", "Duplexes", "Rentals",
            "Apartments"); 

and remove db = this.getWritableDatabase(); from addQuestions , it's redundant and not required.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • 1
    And now I believe even the trim function would work, even if not needed! – Kartik Shandilya Sep 27 '17 at 13:56
  • 1
    yeah you made me look again , nice job :) – Pavneet_Singh Sep 27 '17 at 13:58
  • This helped, but now I think there might be a problem with my database. The "real answer" is stored in as A, B, C, D, so it isn't matching with the given answer. I used a tutorial for this and it looks like it doesn't work. I'm editing my code to include the database – Ang Sep 27 '17 at 14:15
  • @Ang i can't see what `answer.getText().toString()` and `currentQuestion.getAnswer()` is returning , can you provide more details , can you post their values (uselogs/toast) – Pavneet_Singh Sep 27 '17 at 14:30
  • Sure I updated the else statement in checkAnswer to be this: `else { Toast.makeText(getApplicationContext(), "Chosen answer " + answer.getText().toString() + " Real answer " + currentQuestion.getAnswer(), Toast.LENGTH_LONG).show(); }` For the first question, for example, it'll print out "Chosen answer 1 Real Answer A" @Pavneet_Singh – Ang Sep 27 '17 at 14:35
  • In my database helper, I also tried changing the answer where it says "A" "B", etc, to say the actual answer, but the "Real Answer" I print out still says "A" – Ang Sep 27 '17 at 14:39
  • @Ang seems like you have given text to your radiobuttons as numeric values , you can change them to alphabetic other wise ,change it in DB as `....sh", "Hungarian", "3");` , **Note** : the answer to your previous comment is , uninstall your app manually then run it again from studio ,because DB created only once then only updated by changing it's version – Pavneet_Singh Sep 27 '17 at 14:42
  • @Pavneet_Singh Sorry if it was confusing, but for the Chosen Answer for question one, it is "1" because that's what I put in the database. For example, question two says: "Chosen answer Apartments Real answer D". Does that make sense? I uninstalled the app, and now it immediately crashes with this error: "java.lang.IllegalStateException: getDatabase called recursively" – Ang Sep 27 '17 at 15:11
  • @Ang for simplicity use `nts", "Houses", "Duplexes", "Rentals", "Rentals");` & remove `db = this.getWritableDatabase();` from `addQuestions` if still not solved then add full exception details in your question – Pavneet_Singh Sep 27 '17 at 15:20
  • @Pavneet_Singh it's fixed! Thank you so much! – Ang Sep 27 '17 at 15:50
  • @Ang yeahhh , i am glad that i could help , happy coding – Pavneet_Singh Sep 27 '17 at 15:52
  • @Pavneet_Singh thanks! I don't understand why I don't need to get a writable database there since I am adding questions to it though. Do you mind explaining really quickly? – Ang Sep 27 '17 at 16:13
  • @Ang because you already have a `SQLiteDataBase db` instance (which can be used to manipulate database ) here `onCreate(SQLiteDatabase db) {` which you are passing to `addQuestions(SQLiteDatabase db)` and further you are passing to `addQuestion(Question quest, SQLiteDatabase db) {` so no need to fetch a new one when you already have one , [an e.g of references in java](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value), my apology, was not able to response quick , was a busy day – Pavneet_Singh Sep 28 '17 at 14:39
  • @Pavneet_Singh ahh, that makes sense. No problem thanks for the response! – Ang Sep 28 '17 at 20:17