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.
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. Please let me know if there is more code that I can post.
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 = "";
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);"
);
protected 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) {
}
The following code pertains to 'FindingOpponentActivity', the users are matched here, and the list of words is selected in this activity. After the list is chosen, it is converted to GSON to be stored in the SQLite database.
public class FindingOpponentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_find_opponent);
Collection<String> wordList = new LinkedList<String>();
mSelectedWords = new LinkedHashSet<String>();
wordList.add("ant");
wordList.add("almond");
//lots more words
mCopy = new LinkedList<String>(wordList);
Collections.shuffle(mCopy);
mSelectedWords.addAll(mCopy.subList(1, 7));
Gson gson = new Gson();
wordsContainer = gson.toJson(mSelectedWords);
//more code
}
@Override
protected void onResume() {
super.onResume();
//other code
SQLiteOpenHelper wordsListDatabaseHelper =
new WordsListDatabaseHelper(FindingOpponentActivity.this);
SQLiteDatabase db = wordsListDatabaseHelper.getWritableDatabase();
WordsListDatabaseHelper.insertWordList(db,
wordsContainer,mOpponent.getUsername());
After the opponent has been found and the word list is selected, an intent is fired to switch to 'PlayGameActivity'.
code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
mSelectedWords = new LinkedHashSet<String>();
//create a cursor
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);
mCopy = new LinkedList<String>();
mCopy.addAll(finalWordList);
word1 = (TextView) findViewById(R.id.word1);
word1.setText(mCopy.get(1));
// more words
}
cursor.close();
db.close();
} catch (SQLiteException e) {
Toast toast = Toast.makeText(this, "Data unavailable", Toast.LENGTH_LONG);
Log.d(TAG, e.getMessage());
toast.show();
}
Thank you if you read all of this, I know it's quite the lengthy post. Additional code available upon request
Aucun commentaire:
Enregistrer un commentaire