【问题标题】:SharedPreferences is not changingSharedPreferences 没有改变
【发布时间】:2016-01-16 09:52:57
【问题描述】:

我有一个显示简单 2 个 TextViews 的活动,并从 位于另一个类中的字符串数组。 我试图在加载时加载数组中的第一个字符串,然后在下次加载活动时将 textview 中的值更改为我首先在 textView 上显示的字符串之后的字符串,依此类推。 我更改了 sharedprefrences 的值,但它仍然只显示第一个字符串。问题是什么?我更改了字符串数组的索引值,但它仍然只显示第一个字符串。 提前致谢。

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();

}


}

这是另一个类的字符串:

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

【问题讨论】:

    标签: java android string android-sharedpreferences


    【解决方案1】:

    这是因为每次你使用dayForTitle = dayForTitle++;添加这个变量,但dayForTitle++会返回原始值0,所以你只需将dayForTitle = dayForTitle++;改为单个dayForTitle++

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 2014-08-16
      • 1970-01-01
      • 2014-06-17
      • 2014-06-22
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      相关资源
      最近更新 更多