3

I am creating my first android app. It is going to be a quiz app.

There will be 40 categories of questions and every category will have its own database table.

I was provided with amazing code to add questions to my database by an experienced java developer. Unfortunately, I cannot get his code to work.


Enums.java
public class Enums {
    public enum QandAkey {
        KEY_QUESTION, KEY_CORRECT, KEY_ANSWER_A, KEY_ANSWER_B;
    }
    public enum QuestionKey {
       ace_Question1, ace_Question2;
    }
}

Strings.xml

<resources>
    <!-- Question 1 -->
    <string name="ace_Question1.KEY_QUESTION">Question 1?</string>
    <string name="ace_Question1.KEY_CORRECT">KEY_ANSWER_A</string>
    <string name="ace_Question1.KEY_ANSWER_A">Option 1</string>
    <string name="ace_Question1.KEY_ANSWER_B">Option 2</string>

    <!-- Question 2 -->
    <string name="ace_Question2.KEY_QUESTION">Question 2?</string>
    <string name="ace_Question2.KEY_CORRECT">KEY_ANSWER_B</string>
    <string name="ace_Question2.KEY_ANSWER_A">Option 1</string>
    <string name="ace_Question2.KEY_ANSWER_B">Option 2</string>
</resources>

DatabaseHelper.java

// Table Names
private static final String TABLE_ACE = "ace";

// General question columns
private static final String ID = "id"; // question id
private static final String QUES = "question"; // the question
private static final String OPTA = "opta"; // option a
private static final String OPTB = "optb"; // option b
private static final String ANSWER = "answer"; // correct option

private SQLiteDatabase database;

// Create Table ace
private static final String CREATE_TABLE_ACE = "CREATE TABLE IF NOT EXISTS " + TABLE_ACE + "( "
        + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + QUES
        + " TEXT, "+OPTA +" TEXT, " +OPTB +" TEXT, " + ANSWER+ " TEXT)";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // creating required tables
    database = db;
    db.execSQL(CREATE_TABLE_ACE);

    // Add Questions
    addQuestions();
}

private void addQuestions() {
    for(Enums.QuestionKey qk : Enums.QuestionKey.values()) {
        String[] questionAndAnswers = new String[Enums.QandAkey.values().length];
        for(Enums.QandAkey qak : Enums.QandAkey.values()){
            questionAndAnswers[qak.ordinal()]
                    = MainActivity.getContext().getResources()
                        .getString(qk.name()+"."+qak.name()); // Error in this line!
        }
        // let the Question-constructor deal with the strings array itself
        addACEQuestion(new Question(questionAndAnswers));
    }
}

// Add ACE Questions
public void addACEQuestion(Question quest) {
    addQuestion(quest, TABLE_ACE);
}

public void addQuestion(Question quest, String table) {
    //SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(QUES, quest.getQUESTION());
    values.put(OPTA, quest.getOPTA());
    values.put(OPTB, quest.getOPTB());
    values.put(ANSWER, quest.getANSWER());
    // Inserting Rows
    database.insert(table, null, values);
}

Question.java

public class Question {
    private int ID;
    private String QUESTION;
    private String OPTA;
    private String OPTB;
    private String ANSWER;

    public Question(String... questionAndAnswers) {
       QUESTION = questionAndAnswers[Enums.QandAkey.KEY_QUESTION.ordinal()];
       OPTA =questionAndAnswers[Enums.QandAkey.KEY_ANSWER_A.ordinal()];
       OPTB = questionAndAnswers[Enums.QandAkey.KEY_ANSWER_B.ordinal()];
       ANSWER = questionAndAnswers[Enums.QandAkey.valueOf(
            questionAndAnswers[Enums.QandAkey.KEY_CORRECT.ordinal()]
    ).ordinal()
            ];
}





I am trying to get the string resources using the for loop in the DatabaseHelper class.

The Error Message:

Error:(67, 29) error: no suitable method found for getString(String) method Resources.getString(int) is not applicable (argument mismatch; String cannot be converted to int) method Resources.getString(int,Object...) is not applicable (argument mismatch; String cannot be converted to int)

Why are qk.name() and qak.name() integers and not strings? Or do I misunderstand the error message?


I am really new to this and would be tremendously thankful for any kind of help!
Schwesi
  • 4,753
  • 8
  • 37
  • 62
  • 3
    You have it backwards... qk.name() and qak.name() do return strings; Concatenating them together with "." would convert to string even if they weren't. The error message is indicating that the Resources.getString() method expects an integer, not a string. – Jamie Nov 07 '16 at 15:35
  • Aha!! That already helps me a lot! How do I solve this problem? `Integer.parseInt(qk.name()+"."+qak.name())` gives me this error: `java.lang.NumberFormatException: Invalid int: "ace_Question1.KEY_QUESTION"` – Schwesi Nov 07 '16 at 15:39
  • 1
    It appears that you are attempting to load strings from the resources in Android, by name instead of index. See this answer here: http://stackoverflow.com/a/11595723/1385083 – Jamie Nov 07 '16 at 15:42
  • Please don't mutate your original question when new problems turn up. If there is a new unrelated problem post a new question. – Henry Nov 07 '16 at 16:17
  • @Jamie Thank you!! That link helped me tremendously! – Schwesi Nov 07 '16 at 16:23

0 Answers0