gpt4 book ai didi

android - SharedPreferences 的问题

转载 作者:行者123 更新时间:2023-11-29 16:47:36 26 4
gpt4 key购买 nike

我是 android 开发的新手。我正在尝试制作一个简单的应用程序来了解一些事情是如何工作的。我让基本应用程序开始工作,它只有几个 TextView 元素,这些元素通过按钮递增和递减。它是一个基本的记分员。

我遇到的问题是,无论何时关闭程序、旋转屏幕或关闭数据都会丢失。

我正在尝试实现共享首选项以“保存”数据。有 3 个变量保存 Home_Score、Visitor_Score 和 Inning_Number。

我卡住了,因为数据没有正确恢复,我无法弄清楚发生了什么。

Inning_Number = 1
Home_Score = 2
Visitor_Score = 3

onResume 显示数据:

Inning_Number = 0
Home_Score = 1
Visitor_Score = 3

看来第一个字段的值移到了第二个字段,第三个字段是正确的。

我试过很多方法,结果都是一样的。我以为我混淆了变量,但如果我混淆了,我就看不到了。我卡住了,希望有人能给出答案。

提前致谢!

public class MainActivity extends AppCompatActivity {

// Create Variables to hold Field Data
Button btnIncrementHome;
Button btnDecrementHome;
Button btnIncrementVisitor;
Button btnDecrementVisitor;
Button btnIncrementInning;
Button btnDecrementInning;
Button btnReset;

TextView txthomeScore;
TextView txtvisitorScore;
TextView txtinningNumber;

//private SharedPreferences score;
private static final String Game_Score_FileName = "gameScore";
private String Home_Score = "txthomeScore";
private String Visitor_Score = "txtvisitorScore";
private String Inning_Number = "txtinningNumber";

SharedPreferences score;




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

//Set default value in SharedPreferences file
score = getSharedPreferences(Game_Score_FileName, Context.MODE_PRIVATE);
//txthomeScore.setText(score.getString("Home_Score", "0"));
// txtvisitorScore.setText(score.getString("Visitor_Score", "0"));
// txtinningNumber.setText(score.getString("Inning_Number", "0"));

// Get Values of the Form Fields
btnIncrementHome = findViewById(R.id.buttonIncrementHome);
btnDecrementHome = findViewById(R.id.buttonDecrementHome);
btnIncrementVisitor = findViewById(R.id.buttonIncrementVisitor);
btnDecrementVisitor = findViewById(R.id.buttonDecrementVisitor);
btnIncrementInning = findViewById(R.id.buttonIncrementInning);
btnDecrementInning = findViewById(R.id.buttonDecrementInning);
btnReset = findViewById(R.id.buttonReset);
txthomeScore = findViewById(R.id.homeScore);
txtvisitorScore = findViewById(R.id.visitorScore);
txtinningNumber = findViewById(R.id.inningNumber);


btnIncrementHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String homeScoreValue = txthomeScore.getText().toString();
int inthomeScoreValue = Integer.parseInt(homeScoreValue);
inthomeScoreValue++;
txthomeScore.setText(String.valueOf(inthomeScoreValue));
}
});

btnDecrementHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String homeScoreValue = txthomeScore.getText().toString();
int inthomeScoreValue = Integer.parseInt(homeScoreValue);
inthomeScoreValue--;
if (inthomeScoreValue < 0) {
inthomeScoreValue=0;
}
txthomeScore.setText(String.valueOf(inthomeScoreValue));
}
});

btnIncrementVisitor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String visitorScoreValue = txtvisitorScore.getText().toString();
int intvisitorScoreValue = Integer.parseInt(visitorScoreValue);
intvisitorScoreValue++;
txtvisitorScore.setText(String.valueOf(intvisitorScoreValue));
}
});

btnDecrementVisitor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String visitorScoreValue = txtvisitorScore.getText().toString();
int intvisitorScoreValue = Integer.parseInt(visitorScoreValue);
intvisitorScoreValue--;
if (intvisitorScoreValue < 0) {
intvisitorScoreValue=0;
}
txtvisitorScore.setText(String.valueOf(intvisitorScoreValue));
}
});

btnIncrementInning.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String inningNumberValue = txtinningNumber.getText().toString();
int intinningNumberValue = Integer.parseInt(inningNumberValue);
intinningNumberValue++;
txtinningNumber.setText(String.valueOf(intinningNumberValue));
}
});

btnDecrementInning.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String inningNumberValue = txtinningNumber.getText().toString();
int intinningNumberValue = Integer.parseInt(inningNumberValue);
intinningNumberValue--;
if (intinningNumberValue < 0) {
intinningNumberValue=0;
}
txtinningNumber.setText(String.valueOf(intinningNumberValue));
}
});

btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


txthomeScore.setText(String.valueOf(0));
txtvisitorScore.setText(String.valueOf(0));
txtinningNumber.setText(String.valueOf(0));
}
});


}

@Override
protected void onPause(){
super.onPause();

String homeScore = txthomeScore.getText().toString();
String visitorScore = txtvisitorScore.getText().toString();
String inningNumber = txtinningNumber.getText().toString();

score = getSharedPreferences(Game_Score_FileName, MODE_PRIVATE);
SharedPreferences.Editor editor = score.edit();

editor.putString("Inning_Number",inningNumber);
editor.putString("Visitor_Score",visitorScore);
editor.putString("Home_Score",homeScore);
editor.apply();

}

@Override
protected void onResume(){
super.onResume();

score = getSharedPreferences(Game_Score_FileName, MODE_PRIVATE);

String Home = score.getString("Home_Score","0");
String Visitor = score.getString("Visitor_Score","0");
String Inning = score.getString("Inning_Number","1");

txthomeScore.setText(Home);
txtvisitorScore.setText(Visitor);
txthomeScore.setText(Inning);

}



}

最佳答案

你设置了两次主场得分

txthomeScore.setText(Home);  <---- 
txtvisitorScore.setText(Visitor);
txthomeScore.setText(Inning); <----

替换为

txthomeScore.setText(Home);
txtvisitorScore.setText(Visitor);
txtinningNumber.setText(Inning);

由于您是 Android 新手,一般标准使用 camelCase 表示局部变量

关于android - SharedPreferences 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47407732/

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