aspose file tools*
The moose likes Android and the fly likes beginner SharedPreferences, any ready made code open source Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "beginner SharedPreferences, any ready made code open source" Watch "beginner SharedPreferences, any ready made code open source" New topic
Author

beginner SharedPreferences, any ready made code open source

Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
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

Joined: Jan 31, 2010
Posts: 403
given an array of int[5] (5 top scores) how can insert in shared pref file(this after delete/clear contents)?

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You can store them as 5 individual integers.
Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jan 31, 2010
Posts: 403

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jan 31, 2010
Posts: 403
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
Sorry, I don't understand what you're asking; can you rephrase it?
Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 403
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

Joined: Jan 31, 2010
Posts: 403
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.
 
subject: beginner SharedPreferences, any ready made code open source