2

嗨,我正在制作一张显示 4 名玩家在游戏中得分的记分卡,当用户按下按钮时,将插入一个新行。我这样做:

   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

然后它在说下提示错误putString

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

似乎一次只能存储2个变量?(即插槽和 P1Score)。

有 4 位玩家,我想保存他们各自的分数,我该怎么做?

4

5 回答 5

1

putString只接受两个参数:

  • 第一个参数取“关键”
  • 第二个参数取值”

你应该使用:

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

为了得到你的分数。

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

欲了解更多信息:http: //developer.android.com/guide/topics/data/data-storage.html

于 2012-09-15T15:25:36.077 回答
0

.putString()只有 2 个参数。第一个是键,第二个是值。因此,您必须执行以下操作:

SharedPreferences.Editor preferencesEditor = SavedSlots.edit();
for(int i = 1; i<=4; i++){
      preferencesEditor.putString("player_" + i, HisScore);
}
preferencesEditor.apply(); // store the updated preferences
于 2012-09-15T15:21:34.650 回答
0

SharedPreferences 解决方案就像一张地图。一对一。VALUE_KEY

您可以通过将字符串与分隔符连接来解决此问题:

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

检索时:

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...

另请注意,您需要唯一的 KEY 来检索行。

但它真的很乱。您可以使用SQLiteDatabase,您可以根据需要向键(索引列)添加任意数量的值(列)。

于 2012-09-15T15:23:07.233 回答
0

方法 putString 不能像错误所说的那样使用...

“SharedPreferences.Editor 类型中的 putString(String, String) 方法
不适用于参数(字符串、字符串、字符串、字符串、字符串)。”

SharedPreferences.Editor putString(字符串键,字符串值)

于 2012-09-15T15:23:09.230 回答
0

如果要存储 4 个分数,则需要调用 putString 方法 4 次。它应该是这样的:

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

这是因为 putString 方法参数是键和值,而不是您尝试做的键和 4 个值。

另请注意,如果您将使用相同的密钥,下次您将应用分数时,它将覆盖现有的分数,我不确定您要达到什么目标,但它仅适用于 Last Score System 或类似的东西。

于 2012-09-15T15:26:04.240 回答