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