Android的份额preference [英] android sharepreference

查看:119
本文介绍了Android的份额preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我工作呈现4个球员在一场比赛中得分的记分卡,当用户preSS一个按钮,一个新的行会被插入。我通过这样做:

Hi I am working on a scorecard showing 4 players' score in a game, and when the user press a button, a new row would be inserted. I do this by:

   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

然后提示超时错误

It then prompt out error

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

看来只能存储2个变量在同一时间? (即插槽和P1Score)。

It seems that can only store 2 variables at one time? (i.e slot and P1Score).

有4名球员,我想以保存各自的分数,我该怎么办呢?

There are 4 players and I would like to save for their respective scores, how can I do so?

推荐答案

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/指南/主题/数据/数据​​storage.html

这篇关于Android的份额preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆