SharedPreferences&变量交换

时间:2017-12-05 01:44:58

标签: java android sharedpreferences

基本上我每天都会得到一个int和一个String,我保存到SharedPreferences

我想要的是那些变量在每个新的一天改变它们的值,从CurrentDay>传递出来。第+ 1天>第+ +天>第+ 3天......我需要保存它们。

例如,让我们说今天的颜色是红色

// Not actual coding
DAY            -         STATE
Today : Red > 0 > 0 > 0
Tomorrow : NewColor1 > Red > 0 > 0
2 days later : NewColor2 > NewColor1 > Red > 0
3 days later : NewColor3 > NewColor2 > NewColor1 > Red
4 days later : NewColor4 > NewColor3 > NewColor2 > NewColor1 (Red is gone, overwritten by NewColor1)

例如:

SharedPreferences mPreferences;

private String mTextA, mTextB, mTextC;
private int mColor1, mColor2, mColor3;

private String mCurrentText, mYesterdayText, mTwoDaysAgoText, mThreeDaysAgoText;
private int mCurrentColor, mYesterdayColor, mTwoDaysAgoColor, mThreeDaysAgoColor;

//String keys for SharedPreferences storage (int values)
private static final String CURRENT_COLOR = "CURRENT_COLOR";
private static final String YESTERDAY_COLOR = "YESTERDAY_COLOR";
private static final String TWO_DAYS_AGO__COLOR = "TWO_DAYS_AGO_COLOR";
private static final String THREE_DAYS_AGO_COLOR = "THREE_DAYS_AGO_COLOR";

//String keys for SharedPreferences storage (String values)
private static final String CURRENT_TEXT = "CURRENT_TEXT";
private static final String YESTERDAY_TEXT = "YESTERDAY_TEXT";
private static final String TWO_DAYS_AGO_TEXT = "TWO_DAYS_AGO_TEXT";
private static final String THREE_DAYS_AGO_TEXT = "THREE_DAYS_AGO_TEXT";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

mPreferences = getPreferences(MODE_PRIVATE);
mPreferences.edit().putInt(CURRENT_COLOR, mCurrentColor).apply();
mPreferences.edit().putInt(CURRENT_TEXT, mCurrentText).apply();

// This is where I'm having troubles with the logic 

     if (newday == true) {

            // Day 3
            mThreeDaysAgoColor = getPreferences(MODE_PRIVATE).getInt(TWO_DAYS_AGO_COLOR, 0);
            mThreeDaysAgoText = getPreferences(MODE_PRIVATE).getString(TWO_DAYS_AGO_TEXT, null);

            // Day 2
            mTwoDaysAgoColor = getPreferences(MODE_PRIVATE).getInt(YESTERDAY_COLOR, 0);
            mTwoDaysAgoText = getPreferences(MODE_PRIVATE).getString(YESTERDAY_TEXT, null);

            // Yesterday
            mCurrentColor = getPreferences(MODE_PRIVATE).getInt(CURRENT_COLOR, 0);
            mCurrentText = getPreferences(MODE_PRIVATE).getString(CURRENT_TEXT, null);


            mPreferences.edit().putInt(YESTERDAY_COLOR, mYesterdayColor).apply();
            mPreferences.edit().putInt(TWO_DAYS_AGO_COLOR, m2DaysAgoColor).apply();
            mPreferences.edit().putInt(THREE_DAYS_AGO_COLOR, m3DaysAgoColor).apply();


            mPreferences.edit().putString(YESTERDAY_TEXT, mYesterdayText).apply();
            mPreferences.edit().putString(TWO_DAYS_AGO_TEXT, m2DaysAgoText).apply();
            mPreferences.edit().putString(THREE_DAYS_AGO_TEXT, m3DaysAgoText).apply();

澄清:

  • mCurrentText可以是mTextA,mTextB或mTextC
  • mCurrentColor可以是mColor1,mColor2或mColor3

每当我的应用检测到新的一天>

  • mCurrentText保存为mY yesterdayText
  • mCurrentColor保存为mY yesterdayColor

然后它变成Day + 2,Day + 3等,一切都从一行移动,直到第+ + 4天Day + 3被覆盖。

作为最终结果,我希望能够阅读&保存mY yesterdayText / mY yesterdayColor,mTwoDaysAgoText / mTwoDaysAgoColor&的值。每天都有mThreeDaysAgoText / mThreeDaysAgoColor。

我不知道我是否走错路。我需要一些帮助,我是新手:(

1 个答案:

答案 0 :(得分:0)

我遇到了你的问题,你想要改变颜色的值:

Today's launch : Red > 0 > 0 > 0
Tomorrow's launch : NewColor1 > Red > 0 > 0
2 days later's launch : NewColor2 > NewColor1 > Red > 0
3 days later's launch : NewColor3 > NewColor2 > NewColor1 > Red
4 days later's launch : NewColor4 > NewColor3 > NewColor2 > NewColor1 (Red is gone, overwritten by NewColor1)

为此,您根本不需要更改mCurrentText。 在共享pref中有以下变量(仅解释TEXT):

CURRENT_TEXT, YESTERDAY_TEXT, TWO_DAYS_AGO_TEXT, THREE_DAYS_AGO_TEXT

使用您想要的空值或默认值初始化每个值。

现在每当新的一天,您只需按以下方式覆盖值:

putInt(THREE_DAYS_AGO_TEXT, {get value from shared pref of TWO_DAYS_AGO_TEXT})
putInt(TWO_DAYS_AGO_TEXT, {get value from shared pref of YESTERDAY_TEXT})
putInt(YESTERDAY_TEXT, {get value from shared pref of CURRENT_TEXT})
putInt(CURRENT_TEXT, {get value of new text for today})

...你将获得所需的输出。