mardi 29 septembre 2015

SQLite not loading data upon activity starting in android

Problem: The user is matched with another user, and the game begins. The user is given some words, which are taken from the SQLite database. The problem is, when the activity starts, the area where the words should be is empty. The log does not show any errors. Here is the relevant code.

The database class where all the words are kept:

public class WordsListDatabaseHelper extends SQLiteOpenHelper {

protected static Set<String> mSelectedWords;
private static final String DB_NAME = "GAME_TABLE";
private static final int DB_VERSION = 1;
protected static LinkedList<String> mCopy;
public static int gameNumber = 0;


WordsListDatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE GAME ("
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + "SELECTED_WORDS TEXT, "
                    + "GAME_NUMBER INTEGER);"
    );



    Collection<String> wordList = new LinkedList<String>();
    mSelectedWords = new LinkedHashSet<String>();

    wordList.add("ant");
    wordList.add("almond");
      //lots more words

    mCopy = new LinkedList<String>(wordList);

    Gson gson = new Gson();
    String wordsContainer = gson.toJson(mCopy);
    insertWordList(db, wordsContainer, gameNumber);

}

private static void insertWordList(SQLiteDatabase db, String wordsContainer, int gameNumber) {
    ContentValues wordValues = new ContentValues();

    Gson gson = new Gson();
    Collections.shuffle(mCopy);
    mSelectedWords.addAll(mCopy.subList(1,7));
    wordsContainer = gson.toJson(mSelectedWords);

    wordValues.put("SELECTED_WORDS", wordsContainer);
    wordValues.put("GAME_NUMBER", gameNumber);
    db.insert("GAME", null, wordValues);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

}

Here is the relevant code from FindingOpponentsActivity, which randomly matches the user with an opponent. When the match is created, the user is taken to StartGameActivity.

  Intent intentRand = new Intent(FindingOpponentActivity.this, StartGameActivity. .class);
                    int gameNum = WordsListDatabaseHelper.gameNumber;
                    intentRand.putExtra(
                            StartGameActivity.EXTRA_RAND_OPPONENT,
                            mOpponent.getObjectId());

                    intentRand.putExtra(
                            StartGameActivity.EXTRA_GAME_NUMBER,
                            gameNum);

                    sendPushNotification();
                    startActivity(intentRand);
                }

And now, this is the code in the onCreate() method of StartGameActivity:

  Intent intent = getIntent();
    mGameNum = intent.getIntExtra(EXTRA_GAME_NUMBER, 0);


    //create a cursor

    try {
        SQLiteOpenHelper wordsListDatabaseHelper = new WordsListDatabaseHelper(this);
        SQLiteDatabase db = wordsListDatabaseHelper.getReadableDatabase();
        Cursor cursor = db.query("GAME",
                new String[] {"SELECTED_WORDS"},
                "GAME_NUMBER =? ",
                new String[]{Integer.toString(mGameNum)},
                null, null, null);

        //move to the first record in the Cursor

        if (cursor.moveToFirst()) {
            //get wordList
            String savedWordList  = cursor.getString(0);

            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<String>>() {}.getType();
            ArrayList<String> finalWordList = gson.fromJson(savedWordList, type);
            mCopy = new LinkedList<String>();
            mCopy.addAll(finalWordList);


            word1 = (TextView) findViewById(R.id.word1);
            word1.setText(mCopy.get(1));

            word2 = (TextView) findViewById(R.id.word2);
            word2.setText(mCopy.get(2));

Please let me know if there is any more code that I need to post.

EDIT I forgot to mention that I was getting that the log was registering this error message:

 09-29 13:48:42.572  13689-13720/? E/SQLiteLog﹕ (1) no such table: mmsconfig

I checked over and over again for the problem, but I am unable to find where I am going wrong.

Aucun commentaire:

Enregistrer un commentaire