共享preferences更换数据 [英] SharedPreferences replacement of data

查看:162
本文介绍了共享preferences更换数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序,给了我一些字符串每当我preSS按钮,然后使用共享preferences保存此值。但是,我想限制这种节电功能,因此将只保存最近三年收到的字符串。

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.

的结构是如下:
String一个
是字符串b
字符串C

The structure is of the following: String A String B String C

下一次,当我点击我的按钮,它会记录值转换成字符串A,而旧的字符串A移动到字符串B和以字符串C字符串的B旧值,以及相应地删除字符串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();

问候

这篇关于共享preferences更换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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