【问题标题】:Android save multiple high score key and value in sharedpreferencesAndroid在sharedpreferences中保存多个高分键和值
【发布时间】:2018-03-22 09:57:40
【问题描述】:

您好,我正在尝试将高分玩家的姓名存储在sharedpreferences 中。我希望它成为前 5 名玩家。

现在,如果我尝试在其中存储一个数组,我会收到错误消息:“第二个参数类型错误。找到 '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();
}

如何存储多个玩家的姓名和分数?

提前致谢!

【问题讨论】:

标签: android


【解决方案1】:

您的错误明确表明您需要将 Set 对象传递给函数putStringSet()。如documentation 中所述。

考虑到这一点,我认为使用 SharedPreferences 来存储您的 HighScores 是一个坏主意。你会面临不同的问题。

第一选择:使用玩家名称作为键,我们只使用putString作为我们输入的值

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();
}

这是一个非常糟糕的实现。因为关键是玩家姓名,所以很难找回你的分数。

第二选择:仅使用一个键并将所有分数存储在一个集合中

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();
}

这也不是一个好主意。因为你需要重新定义一个函数来找到较低的分数并将其删除。您还需要定义一个模式来存储名称 + 分数。然后你需要定义一个函数来读取分数以将名称与分数分开。

解决方案:

这里好的解决方案是使用数据库。偏好不是针对存储的数据设计的,而是针对偏好设计的。此外,数据库将提供轻松存储/检索/订购/等您的数据的功能。看看here

【讨论】:

  • “错误的第二个参数类型。发现 'java.lang.String[}' required 'java.util.Set'”是我得到的错误
  • 你写的。那是因为根据 [documentation](developer.android.com/reference/android/content/…, java.util.Set)) 函数需要 Set&lt;String&gt; 对象而不是 Set&lt;String[]
  • 注意这一点,我认为使用 SharedPreferences 存储分数是一种不好的方式。我将编辑我的答案来解释这一点
【解决方案2】:

将您的二维数组转换为字符串并将字符串保存在共享首选项中。

这里是代码

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(); }
}
}

将字符串保存在共享首选项中

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

查看这篇文章了解更多详情

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2012-05-30
    相关资源
    最近更新 更多