SharedPreferences 替换数据

SharedPreferences replacement of data(SharedPreferences 替换数据)
本文介绍了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 替换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)