保存基本统计数据?
Save Basic Statistics data?
我正在制作一款需要将一些统计数据保存到内存中的游戏(玩过的游戏、赢过的游戏等)。
我试过使用 sharedpreferences,但从我所看到的来看,文档似乎很差,而且我无法让它工作。
我错过了什么吗?
try {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
SharedPreferences stats = getPreferences(MODE_PRIVATE);
CharSequence gampjla = "Games played: " + stats;
Toast toast = Toast.makeText(context, gampjla, duration);
toast.show();
gamesPlayed++;
SharedPreferences.Editor editor = stats.edit();
editor.putInt("gameplay", gamesPlayed).commit();
CharSequence gampla = "Games played: " + gamesPlayed;
Toast toayst = Toast.makeText(context, gampla, duration);
toayst.show();
}
catch (Exception e) {
Context context = getApplicationContext();
CharSequence errormsg = "Unable to save!! " + e;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, errormsg, duration);
toast.show();
}
我试过各种变体,但没有任何效果!
感谢您的帮助!
编辑:一些变量名中的随机字母是因为我想要一个不同的变量名来进行测试,而且我感到很沮丧,所以我当时不会被打扰!
改变这个:
CharSequence gampjla = "Games played: " + stats;
至:
CharSequence gampjla = "Games played: " + stats.getInt("gameplay",-1);
已解决!
这是一个愚蠢的问题。我写了 'putInt' 但用了 'getString'.
我正在制作一款需要将一些统计数据保存到内存中的游戏(玩过的游戏、赢过的游戏等)。 我试过使用 sharedpreferences,但从我所看到的来看,文档似乎很差,而且我无法让它工作。 我错过了什么吗?
try {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
SharedPreferences stats = getPreferences(MODE_PRIVATE);
CharSequence gampjla = "Games played: " + stats;
Toast toast = Toast.makeText(context, gampjla, duration);
toast.show();
gamesPlayed++;
SharedPreferences.Editor editor = stats.edit();
editor.putInt("gameplay", gamesPlayed).commit();
CharSequence gampla = "Games played: " + gamesPlayed;
Toast toayst = Toast.makeText(context, gampla, duration);
toayst.show();
}
catch (Exception e) {
Context context = getApplicationContext();
CharSequence errormsg = "Unable to save!! " + e;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, errormsg, duration);
toast.show();
}
我试过各种变体,但没有任何效果! 感谢您的帮助!
编辑:一些变量名中的随机字母是因为我想要一个不同的变量名来进行测试,而且我感到很沮丧,所以我当时不会被打扰!
改变这个:
CharSequence gampjla = "Games played: " + stats;
至:
CharSequence gampjla = "Games played: " + stats.getInt("gameplay",-1);
已解决! 这是一个愚蠢的问题。我写了 'putInt' 但用了 'getString'.