简体   繁体   English

SharedPreferences不变

[英]SharedPreferences is not changing

I have an activity that shows simple 2 TextViews, and get their text from a string array that located in another class. 我有一个活动,它显示简单的2个TextView,并从位于另一个类中的字符串数组获取其文本。 I am trying to load the first string in the array when loading, and change then the next time the activity is loading change the value in the textview to the string that after the string that I showed first on the textView, and so on. 我试图在加载时在数组中加载第一个字符串,然后在下次活动加载时更改,然后将textview中的值更改为我在textView上首先显示的字符串之后的字符串,依此类推。 I change the value of the sharedprefrences but it still only shows me the first string. 我更改了sharedprefrences的值,但它仍然只显示第一个字符串。 what is the problem? 问题是什么? I change the value of the index of the string array, but it still only shows me the first string. 我更改了字符串数组的索引值,但是它仍然只显示第一个字符串。 Thanks in advanced. 提前致谢。

public class DailyMessage extends AppCompatActivity {
    public SharedPreferences startExplePref;
String[] DescS;
String[] titleS;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_message);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    startExplePref = PreferenceManager.getDefaultSharedPreferences(this);

    boolean isFirstRun = startExplePref.getBoolean("FIRSTRUN", true);
    if (isFirstRun) {
        SharedPreferences.Editor editor = startExplePref.edit().putBoolean("FIRSTRUN", false);
        editor.commit();
        startExplePref.edit().putInt("Day",0).commit();

    }

    TextView titleTV = (TextView)findViewById(R.id.title);
    TextView descTV = (TextView)findViewById(R.id.description);
    TextView dateTV = (TextView)findViewById(R.id.date);
    dateTV.setText(currentDateTimeString);

    int dayForTitle = startExplePref.getInt("Day",0);
    titleTV.setText(DailyMessagesContent.content[dayForTitle]);
    descTV.setText(DailyMessagesContent.content[dayForTitle]);
    dayForTitle = dayForTitle++;
    startExplePref.edit().putInt("Day",dayForTitle).commit();

}


}

this is the string from the other class: 这是另一个类的字符串:

public static String content[] = {"test1", "test2","test3"};

It's beacause everytime you use dayForTitle = dayForTitle++; 因为每次您使用dayForTitle = dayForTitle++; to add this variable,but dayForTitle++ will return primitive value 0,so you just need to change dayForTitle = dayForTitle++; 添加此变量,但是dayForTitle++将返回原始值0,因此您只需要更改dayForTitle = dayForTitle++; to single dayForTitle++ . dayForTitle++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM