• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

beginner SharedPreferences, any ready made code open source

 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SharedPreferences

I want in the current "guess the flag" game in Android 2.3.3...

given at end the score only (for now after I will insert for the score gamer name) use SharedPreferences to store 5 top scores...

you know any ready made code for this, maybe open source?

how read the SharedPreferences file and idenify that is the first time to run...? may populate it with(gamer score _ String int) initially:
gamer1 0
gamer2 0
gamer3 0
gamer4 0
gamer5 0

there after input in file new high scores...
 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given an array of int[5] (5 top scores) how can insert in shared pref file(this after delete/clear contents)?

 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can store them as 5 individual integers.
 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but I want kept during App killed...

can you guest where the error below?
is it here:
preferencesEditor.putInt(String.format("%d",k), scores[k]);

crash App when this function run... no other code part has error?

 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an app crashes, your first step is always to check the logcat output for errors. It looks like you're not initializing the "scores" array.
 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

from array int[5] to shared pref is above correct?

I applied parameter to method current score

private String scoreSaveReturn(int totalPoints) {

but shows* like tags.length=0, how I populated from an array a shared pref file (after clear it)?

*shows= "Top Scores:\n"; // only
 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to start by finding out in which line of code, and at which instruction, you're getting an exception. I pointed out one cause for exceptions; did you fix that?
 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please ignore ALL ABOVE... AFTER DEBUGGING efforts found bug in this:

how from an (i)empty shared pref or (ii)full-5 or (iii)semi full eg 3... I get the int and populate score[] array...?

normally first time run should be empty the shared pref... but may be semi full, any way how populate int[] array???

 
Tim Moores
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't understand what you're asking; can you rephrase it?
 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if shared preferences files is full (for this app 5 (scores five top) string/int values) or has NOTHING in it or has 3 values(str/int),... i GET from this data THE INT VALUES and, populate an array int[5] if null value is only 0 (zero)... well, in other words from shared pref to array int[] is this correct???
 
Leonidas Savvides
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THE 2ND block detected erroneous WHERE THE PROBLEM? 1ST AFTER FOLLOW SECOND IN CODE SEQUENCE



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic