Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将新的键值对附加到现有的共享首选项中_Android_Sharedpreferences - Fatal编程技术网

Android 将新的键值对附加到现有的共享首选项中

Android 将新的键值对附加到现有的共享首选项中,android,sharedpreferences,Android,Sharedpreferences,请告诉我如何将新的键值对附加到现有的共享首选项中。使用以下代码将键值对添加到共享首选项中: SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("key", "value"); editor.putInt("key1", 1); editor.commit(); 但我想将不同活动中的值添加到已存在的首选项中。只要您在此处使用相同的

请告诉我如何将新的键值对附加到现有的共享首选项中。

使用以下代码将键值对添加到共享首选项中:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("key", "value");
editor.putInt("key1", 1);
editor.commit();

但我想将不同活动中的值添加到已存在的首选项中。只要您在此处使用相同的标识符,即“我的首选项”名称,就可以实现此目的。确保在每个活动中输入相同的标识符。也许您可以创建一个实用程序类,为标识符定义一个静态字符串常量,并在所有活动中使用它。如果要添加新的密钥对值,请在其他活动中使用MY_PREFS_NAME。