Reputation:
This question gets thrown around here a lot. However, my side of the implementation is buggy, so it would help if you guys can help me out. Thanks in advance. Sorry if this question is so noob-like.
I develop Court Counter, which can be found here. I recently started to add saving support for my app. However, I'm not sure if I'm doing it correctly. All other stack overflow topics mention Editor, however Android Studio corrects me to SharedPreferences.Editor. I'm assuming this changed as of Marshmallow or whatever.
Anyway, I added these lines of code for saving and loading:
/**
* Saves your current session
* SharedPreferences key = courtCounter
* team A key = teamA
* team B key = teamB
*/
public void saveSession(View v) {
//We will get saved preferences!
SharedPreferences prefs = this.getSharedPreferences("courtCounter", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("teamA",scoreA);
editor.putInt("teamB",scoreB);
editor.commit();
}
/**
* Loads previous session
* The config is same as saveSession
*/
public void loadSession(View v) {
//We will get the preferences!
SharedPreferences prefs = this.getSharedPreferences("courtCounter", Context.MODE_PRIVATE);
scoreA = prefs.getInt("teamA", 0);
scoreB = prefs.getInt("teamB", 0);
}
saveSession is called when the score gets updated, which you can see below:
/**
* New way of adding score
*
* @param score Score that you need to add to the team
* @param team Team that you want the score added to
* @param last Last clicked item, for example, a1. This will be used for the undo action.
*/
public void scoreAdd(int score, char team, String last) {
Button undo = (Button) findViewById(R.id.undo_button);
undo.setEnabled(true);
lastClicked = last;
if (team == 'a') {
scoreA += score;
displayScore(scoreA, 'a');
}
if (team == 'b') {
scoreB += score;
displayScore(scoreB, 'b');
}
saveSession();
}
However, this throws an error, saying that I didn't supply a (view) or whatever in the brackets. How can I fix this? I don't need any input handlers, but Android Studio would freak out if I didn't make one.
Right now, the code works fine over here:
public void resetAll(View view) {
Button undo = (Button) findViewById(R.id.undo_button);
undo.setEnabled(false);
lastClicked = "reset";
scoreA = 0;
scoreB = 0;
displayScore(scoreA, 'a');
displayScore(scoreB, 'b');
saveSession(view);
}
I'm assuming this is because the resetAll has a view input parameter. However, resetAll is called from an onClick.
Other places where code does not work include the onCreate.
Any ideas?
Upvotes: 0
Views: 85
Reputation: 543
Your saveSession is not even using the View v within the method, why are you making it as a parameter? Remove it.
public void saveSession() {
...
}
public void resetAll(View view) {
...
saveSession(); // I remove the view here.
}
You have a lot of unnecessary params, unnecessary since your method doesn't even use it.
Upvotes: 0