lundi 5 octobre 2015

unable to display data from SQLite database in android

Background: I am creating a turnbased multiplayer game. The user is randomly matched with another user. Upon matching, the user is given a list of words, which he will need to use for the reminder of the game. This list of words is randomly chosen from a much larger list.

The relevant classes are:

WordsListDatabaseHelper - contains table which saves the opponent name, and the randomized list of words.

FindingOpponentActivity - Opponent is found and randomly matched with user.

PlayGameActivity- the wordList is retrieved from the database and put here.

WordList- class that contains a long list of words, has a static getRandomWords method that returns a sublist.

Problem: Once the user is matched with another in the FindOpponentActivity, an intent is fired up and a new activity PlayGameActivity, starts. This is the activity where the random list of words is suppose to appear but the area where they are suppose to be is empty. There are no exceptions thrown, the logcat has no weird output to my knowledge.

I set breakpoints on this line of code:

WordsListDatabaseHelper.insertWordList (db, wordsContainer, mOpponent.getUsername()); On debugging, I got the following message: warning: no executable code found in line(this line number)

I also put additional breakpoints on this block of code in PlayGameActivity:

  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);

There were no messages here, but when I double clicked on finalWordList or savedWordList I got the following message: cannot find local variable savedWordList/finalWordList. furthermore, when I double click on mCopy, it shows: mCopy = null;

Here are the relevant codes:

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 String oppName = "";
protected static String EXTRA_OPPONENT_NAME = "OPPONENT_NAME";


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, "
                    + "OPPONENT_NAME TEXT);"

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, "
                    + "OPPONENT_NAME TEXT);"
    );


   public static void insertWordList(SQLiteDatabase db, String wordsContainer, String oppName) {
    ContentValues wordValues = new ContentValues();

    wordValues.put("SELECTED_WORDS", wordsContainer);
    wordValues.put("OPPONENT_NAME", oppName);
    db.insert("GAME", null, wordValues);
}

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

}

This is from FindingOpponentActivity, in the onResume method:

 //add the game values in the SQLite database
                    String wordsContainer = new String();
                    Gson gson = new Gson();
                    wordsContainer = gson.toJson(WordsList.getRandomWords());

                    SQLiteOpenHelper wordsListDatabaseHelper =
                            new WordsListDatabaseHelper(FindingOpponentActivity.this);

                    SQLiteDatabase db = wordsListDatabaseHelper.getWritableDatabase();

                    WordsListDatabaseHelper.insertWordList
                            (db, wordsContainer, mOpponent.getUsername());

Finally, here is the code from the onCreate method of PlayGameActivity:

 Intent randIntent = getIntent();
    mOpponentName = randIntent.getStringExtra(EXTRA_RAND_OPPONENT);
    try {
        SQLiteOpenHelper wordsListDatabaseHelper = new WordsListDatabaseHelper(this);
        SQLiteDatabase db = wordsListDatabaseHelper.getReadableDatabase();
        Cursor cursor = db.query("GAME",
                new String[] {"SELECTED_WORDS"},
                "OPPONENT_NAME =? ",
                new String[]{mOpponentName},
                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);

            protected LinkedList<String> mCopy;
            mCopy = new LinkedList<String>();
            mCopy.addAll(finalWordList);

I know this is a very lengthy post, but I will be eternally grateful for any sort of help. I have been stuck on this problem for an embarrassingly long period of time.

Aucun commentaire:

Enregistrer un commentaire