SharedPreferences replacement of data(SharedPreferences 替换数据)
问题描述
我有一个应用程序,每当我按下按钮时都会给我一些字符串,然后使用 sharedpreferences 保存这个值.但是,我想限制这个保存功能,所以它只会保存最后三个收到的字符串.
I have application that gives me some string whenever I press the button and then save this value using sharedpreferences. However, I would like to limit this saving function, so it will save only the last three received strings.
结构如下:字符串 A字符串 B字符串 C
The structure is of the following: String A String B String C
下次我点击按钮时,它会将值记录到字符串 A 中,同时将旧字符串 A 移动到字符串 B,将字符串 B 的旧值移动到字符串 C,并相应地删除字符串 C 的旧值.
Next time when i click my button it will record the value into String A, while move the old String A to String B and old value of String B to String C, as well as, delete the old value of String C accordingly.
目前我不确定它是如何完成的.
At the moment I'm not sure how its done.
期待您的帮助.
推荐答案
试试这样的:
//Obtain values
SharedPreferences prefs =
getSharedPreferences("PreferencesKey", Context.MODE_PRIVATE);
String stringA = prefs.getString("stringA", "defaultValue");
String stringB = prefs.getString("stringB", "defaultValue");
String stringC = prefs.getString("stringC", "defaultValue");
//Save values
SharedPreferences.Editor editor = prefs.edit();
editor.putString("stringA", lastValueSelected);
editor.putString("stringB", stringA);
editor.putString("stringC", stringB);
editor.commit();
问候
这篇关于SharedPreferences 替换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SharedPreferences 替换数据


- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- android 4中的android RadioButton问题 2022-01-01