pearmak
pearmak

Reputation: 5027

Android SharedPreference

Hi I am working on a scorecard showing 4 players' score in a game, and when the user press a button, a new row would be inserted. I do this by:

   private void makeTag(String P1Score, String P2Score, String P3Score, String P4Score, String slot)
   {
      // originalQuery will be null if we're modifying an existing search
      String originalScore = SavedSlots.getString(slot, null);

      // get a SharedPreferences.Editor to store new slot/scores
      SharedPreferences.Editor preferencesEditor = SavedSlots.edit();
      preferencesEditor.putString(slot, P1Score, P2Score, P3Score, P4Score); // to store
      preferencesEditor.apply(); // store the updated preferences

It then prompt out error under putString saying:

The method putString(String, String) in the type SharedPreferences.Editor is not  
applicable for the arguments (String, String, String, String, String).

It seems that can only store 2 variables at one time? (i.e slot and P1Score).

There are 4 players and I would like to save for their respective scores, how can I do so?

Upvotes: 2

Views: 775

Answers (5)

Rishabh Agrawal
Rishabh Agrawal

Reputation: 901

putString takes only two parameters:

  • 1st argument take "key"
  • 2nd argument take value"

You Should use :

private void makeTag(String P1Score, String P2Score, String P3Score, String P4Score, String slot)
{
    // originalQuery will be null if we're modifying an existing search


    //* For Save your score//

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor preferencesEditor = SavedSlots.edit();

    preferencesEditor.putString("slot1", "P1Score") // to store
    preferencesEditor.putString("slot2", "P2Score") // to store
    preferencesEditor.putString("slot3", "P3Score") // to store
    preferencesEditor.putString("slot4"," P4Score") // to store
    preferencesEditor.commit(); // store the updated preferences

for getting your score.

String  palyer1 = settings.getString("slot1", "notfound);
String  palyer2 = settings.getString("slot2", "notfound);
String  palyer3 = settings.getString("slot3", "notfound);
String  palyer4 = settings.getString("slot4", "notfound);

For More Info: http://developer.android.com/guide/topics/data/data-storage.html

Upvotes: 1

Balázs Édes
Balázs Édes

Reputation: 13807

The SharedPreferences solution works like a map. One VALUE to one KEY.

You can solve this by concatenating your Strings with a separator character:

preferencesEditor.putString(slot, P1Score+"|"+P2Score+"|"+P3Score+"|"+P4Score);

and when retrieving:

String scoreString = preferences.getString(slot, "");
// each item in this array will be one score
String [] scores = scoreString.split("|");

String playerOneScore = scores[0];
String playerTwoScore = scores[1];
//... and so on...

Also note, that you need a unique KEYs to retrieve the rows.

But its really messy. You could use the SQLiteDatabase, where you can add as many values (columns) to a key(index column) as you want.

Upvotes: 0

idanakav
idanakav

Reputation: 1486

If you want to store 4 scores you need to call putString method 4 times. it should be something like this :

preferencesEditor.putString("p1Score", score1);
preferencesEditor.putString("p2Score", score2);
preferencesEditor.putString("p3Score", score3);
preferencesEditor.putString("p4Score", score4);

its because putString method arguments are key and value and not key and 4 values like you tried to do.

also notice that next time you will apply scores if you will use same key it will overwrite existing scores , i'm not sure what you try to achieve but it will be good for only Last Score System or something similar.

Upvotes: 0

BrainCrash
BrainCrash

Reputation: 13172

The method putString can't be used that way as the error say...

"The method putString(String, String) in the type SharedPreferences.Editor is not
applicable for the arguments (String, String, String, String, String)."

SharedPreferences.Editor putString (String key, String value)

Upvotes: 0

PKeidel
PKeidel

Reputation: 2589

.putString() has only 2 arguments. The first one is the key and the second one is the value. So you have to do something like:

SharedPreferences.Editor preferencesEditor = SavedSlots.edit();
for(int i = 1; i<=4; i++){
      preferencesEditor.putString("player_" + i, HisScore);
}
preferencesEditor.apply(); // store the updated preferences

Upvotes: 0

Related Questions