SharedPreferences 替换数据 [英] SharedPreferences replacement of data

查看:25
本文介绍了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 替换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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