gpt4 book ai didi

java - 与sharedpreference共享一个int变量

转载 作者:行者123 更新时间:2023-12-02 03:44:24 26 4
gpt4 key购买 nike

我有一个简单的问题:在第一个 Activity 中,我将变量保存为 int 数字:

valsat2= findViewById(R.id.valsatu);
sharedpreferences = getSharedPreferences(valsat, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
int n = Integer.parseInt(valsat2.getText().toString());
editor.putInt(valsat, n);
editor.commit();

在第二个 Activity 中,我想使用这个 int 数字,但如果我使用此代码,它会返回给我 0:

public static final int valsat = 0 ;
TextView sat = (TextView) findViewById(R.id.valoresaturazione);
sharedpreferences = getSharedPreferences("valsat", Context.MODE_PRIVATE);
int valsatn = sharedpreferences.getInt("valsat", 0);
sat.setText(Integer.toString(valsatn));

如何解决?谢谢!

最佳答案

您必须将值保存到“valsat”字符串标签。像这样改变这部分 -

valsat2= findViewById(R.id.valsatu);
sharedpreferences = getSharedPreferences(valsat,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
int n = Integer.parseInt(valsat2.getText().toString());
editor.putInt("valsat", n);
editor.commit();

关于java - 与sharedpreference共享一个int变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56817817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com