Leonidas Savvides
- Forum posts: 45
Jun 17, 2012, 3:06:34 PM via Website
Jun 17, 2012 3:06:34 PM via Website
Want: store and appear 5 top scores of a Game. Problem reading/writing to an array from Shared Preferences(Sh Pref).
scoreSaveReturn():
Get from Sh Pref store in array int[5], delete all Sh Pref, if score appropriate add it to array, populate Sh Pref and return top five scores as string.
THE 2ND block detected erroneous (if disabled - works with fixed value returned) WHERE THE PROBLEM? 1ST AFTER FOLLOW SECOND IN CODE SEQUENCE
scoreSaveReturn():
Get from Sh Pref store in array int[5], delete all Sh Pref, if score appropriate add it to array, populate Sh Pref and return top five scores as string.
THE 2ND block detected erroneous (if disabled - works with fixed value returned) WHERE THE PROBLEM? 1ST AFTER FOLLOW SECOND IN CODE SEQUENCE
1// this block detected NO ERROR
2
3savedHighScores = getSharedPreferences("highScores", MODE_PRIVATE);
4
5private String scoreSaveReturn(int totalPoints) {
6 SharedPreferences.Editor preferencesEditor = savedHighScores.edit();
7 String[] tags = savedHighScores.getAll().keySet().toArray(new String[0]);
8 int[] scores = new int[5];
9....................
same method(scoreSaveReturn) continues below2
3savedHighScores = getSharedPreferences("highScores", MODE_PRIVATE);
4
5private String scoreSaveReturn(int totalPoints) {
6 SharedPreferences.Editor preferencesEditor = savedHighScores.edit();
7 String[] tags = savedHighScores.getAll().keySet().toArray(new String[0]);
8 int[] scores = new int[5];
9....................
1...............
2 // this block detected erroneous WHERE THE PROBLEM? Tried to populate array int[5] from Shared Pref.
3for (int k = 0; k<5; k++) {
4 scores[k]=savedHighScores.getInt(tags[k],0);
5}
6 preferencesEditor.clear();
7 preferencesEditor.apply();
.............other code works OK. 2 // this block detected erroneous WHERE THE PROBLEM? Tried to populate array int[5] from Shared Pref.
3for (int k = 0; k<5; k++) {
4 scores[k]=savedHighScores.getInt(tags[k],0);
5}
6 preferencesEditor.clear();
7 preferencesEditor.apply();
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.