0

Hello I am trying to store the name of the player with the high score in the sharedpreferences. I want it to be the top 5 players.

Now if I try to store an array in there I get the error: "Wrong 2nd argument type. Found 'java.lang.String[][]' required 'java.util.Set<java.lang.String>'"

public static void setHighScore(Context context, String name, int score) {
    String[] player = new String[] {name, String.valueOf(score)};
    String[][] highScores = new String[][] {player};
    SharedPreferences.Editor editor = getPreferences(context).edit();
    editor.putStringSet(PLAYER, highScores);
    editor.apply();
}

How can I store a name and a score of multiple players?

Thanks in advance!

Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
ivan franken
  • 21
  • 1
  • 7
  • 5
    Possible duplicate of [Is it possible to add an array or object to SharedPreferences on Android](https://stackoverflow.com/questions/3876680/is-it-possible-to-add-an-array-or-object-to-sharedpreferences-on-android) – Thomas Mary Mar 22 '18 at 09:59

2 Answers2

1

Your error says explicitly that you need to pass a Set object to the function putStringSet(). As explained in the documentation.

Reguarding to this, I think using SharedPreferences to store your HighScores is a bad idea. You will face different problems.

First choice : Use the player name as key, use just putString as we put one value

public static void setHighScore(Context context, String name, int score) {
    Set<String> scoreSet = new HashSet<String>();
    scoreSet.add(String.valueOf(score));
    SharedPreferences.Editor editor = getPreferences(context).edit();
    editor.putString(name, scoreSet);
    editor.apply();
}

This is a really bad implementation. Because it will be hard to retrieve your score since the key is the player name and will always change.

Second Choice : Use only one key and store all the score in a Set

public static void setHighScore(Context context, String name, int score) {
    SharedPreferences prefs = getPreferences(context);
    Set<String> scoreSet = prefs.getStringSet("highScores"); //I use "highScores" as the key, but could be what you want
    // You need to create a function that find the lower scores and remove it
    removeLower(scoreSet);
    scoreSet.add(name + ":" + String.valueOf(score)); //need to define a pattern to separate name from score
    SharedPreferences.Editor editor = prefs.edit();
    editor.putStringSet("highScores", scoreSet);
    editor.apply();
}

This is also not a good idea. Because you need to redefine a function to find the lower score and remove it. You also need to define a pattern to store name + score. Then you need to define a function to read scores to separate the name from the score.

Solution :

The good solution here is to use a database. Preferences are not design to stored data but preferences. Also a database will provides functionality to easily store/retrieve/order/etc your data. Have a look here

vincrichaud
  • 2,218
  • 17
  • 34
  • "Wrong 2nd argument type. Found 'java.lang.String[}' required 'java.util.Set'" is the error i get – ivan franken Mar 22 '18 at 10:20
  • Your write. That because acording to the [documentation](https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putStringSet(java.lang.String, java.util.Set)) the function expect a `Set` object and not a `Set – vincrichaud Mar 22 '18 at 10:27
  • Reguarding to this I think that it's a bad way to store your scores by using SharedPreferences. I'll edit my answer to explain this – vincrichaud Mar 22 '18 at 10:28
0

Convert your double dimension array to string and save the string in shared preference.

Here is the code

private String twoDimensionalStringArrayToString(String[][] s) throws UnsupportedEncodingException, IOException {
ByteArrayOutputStream bo = null;
ObjectOutputStream so = null;
Base64OutputStream b64 = null;
try {
    bo = new ByteArrayOutputStream();
    b64 = new Base64OutputStream(bo, Base64.DEFAULT);
    so = new ObjectOutputStream(b64);
    so.writeObject(s);
    return bo.toString("UTF-8");
} finally {
    if (bo != null) { bo.close(); }
    if (b64 != null) { b64.close(); }
    if (so != null) { so.close(); }
}
}

Save the string in Shared preference

prefsEditor.putString(PLAYLISTS, sb.toString());

Check this post for further details

how to store 2dimensional array in shared preferences in android or serialize it

Fazal Hussain
  • 1,129
  • 12
  • 28