Stuck dealing with intents across activities

  • Replies:6
Richard Hall
  • Forum posts: 2

Jul 31, 2017, 2:20:01 AM via Website

Hi all,

I'm struggling to pass intents to activities. I have "select player 1", "select player 2" activities. You select a name in each one, and in the "start game" activity, "Player 1" , "Player 2" should update with the names (along with passing the userID) ... but whenever I update one it defaults the other back to the original text
As I'm a new user, I can't link to the github but it as found at -> rhall91/Project4

Reply
Dev Lt
  • Forum posts: 54

Jul 31, 2017, 3:35:05 PM via Website

I downloaded your project and tested it out. I was keep getting this error:
android.database.sqlite.SQLiteException: table player_table already exists (code 1): , while compiling: CREATE TABLE player_table (ID INTEGER PRIMARY KEY AUTOINCREMENT, playerIDINT, wins TEXT, losses INT, tiesINT )

so the first thing you need to do is to change your create table sentences to these:
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + Col2 + " TEXT)";
and
createTable = "CREATE TABLE IF NOT EXISTS " + TABLE2_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + Col2_2 + "INT, "
+ Col3_2 + " TEXT, " + Col4_2 + " INT, " +Col5_2+ "INT )";
Then explain your problem again in simple english cuz it's hard to understand now. Maybe try to avoid technical terms.

Reply
Richard Hall
  • Forum posts: 2

Jul 31, 2017, 5:49:57 PM via Website

Hi Dev, updated the code as suggested.

Basically, if you select player 1, it takes you to the "game screen". You need to get back to the main menu and select player 2. Hitting the back button to get to the main menu, I then select player 2, which forwards you again to the "game screen" however, player 1 is no longer set. I need both users to be set so I can continue with the "game"

Reply
Dev Lt
  • Forum posts: 54

Jul 31, 2017, 6:54:16 PM via Website

Basically, instead of passing data to intents I would suggest to use SharedPreferences. You can save player names there and when your game starts retrieve these values. When game ends you just remove it so when new game beggins players would have to enter their names again.

Setting values:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.apply();

Retrieving values:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("name", null);
if (restoredText != null) {
String name = prefs.getString("name", null);//null is the default value.
if (name != null) {
// name is set
}
}

Reply
John Wiggins
  • Forum posts: 3

Aug 1, 2017, 5:28:07 AM via Website

You changed your code to "CREATE TABLE IF NOT EXISTS". You must put a space after EXISTS. Your program crashes now since the word EXISTS runs up against the table name.

07-31 23:08:05.324 2208-2208/com.example.rhall.project4 E/SQLiteLog: (1) near "EXISTSplayer_table": syntax error

Reply
John Wiggins
  • Forum posts: 3

Aug 1, 2017, 11:22:28 PM via Website

OK, I got a chance to play with your app and look over your code. I don’t believe passing data to intents is the problem, but you should be doing as Dev Lt suggests and use SharedPreferences. You are counting on the setting of text on a TextView object to save information for you. I am not up on how TextView objects work enough to know exactly when they lose their text, but I suspect that the use of the back button is what is causing the loss of this text.
Whether that is correct or not (if anyone knows, I would love to read about it), SharedPreferences is the way to go.

For example :

private SharedPreferences sharedPref;
sharedPref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("PLAYER1_NAME", player1);
editor.commit();

Reply
John Wiggins
  • Forum posts: 3

Aug 3, 2017, 5:09:59 AM via Website

Did you try SharedPreferences? If you are having problems with that, you could also put the data into the SQLite database.

Reply