重新启动应用程序时,如何在EditText中保持string/int值



在我的Android活动中,我有一个EditText、一个'+'按钮、一个'-'按钮、'保存按钮和'>加载’按钮。当我按下">+"时,EditText中的值会增加1,类似地,按下">
-"时,值会减少1。单击"保存"时,我使用SharedPreferences保存数据。当我单击"加载"时,我想将此数据重新加载到EditText字段中。

现在的问题是,当我完全退出应用程序(即使是最近使用的应用程序(,并在重新启动时单击"加载"时,保存的数字不会出现。我在onRestart()方法中包含了"Load"方法的onClick()操作。它仍然不起作用。我在这里错过了什么?对于之前在这里提出的类似问题,我甚至尝试了所有其他建议。另外,它真的是onRestart()还是onRestoreInstanceState()

public class MainActivity extends Activity {
Button btn1;
Button btn2;
Button btn3;
Button btn4;
EditText scoreText;
int counter = 0;
TextView textTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.add);
btn2 = (Button)findViewById(R.id.subtract);
btn3 = (Button)findViewById(R.id.save);
btn4 = (Button)findViewById(R.id.load);
scoreText = (EditText)findViewById(R.id.total);
textTitle = (TextView)findViewById(R.id.title);
btn1.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View v) {
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View v) {
counter=counter-1;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View v) {
//store data using sharedprefernces
SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
//Edit method allow to write the data in sharedpreferences
editor.putString("count",scoreText.getText().toString());
//For commit changes commit() method is used
editor.commit();
Toast.makeText(getApplicationContext(),"Data Saved",Toast.LENGTH_SHORT).show();
}
});
btn4.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
String strcount=sharedPreferences.getString("name",scoreText.getText().toString());
// scoreText.setText(strcount);
scoreText.setBackgroundColor(Color.YELLOW);
}
});
}
@Override
protected void onRestart(Bundle savedInstanceState){
super.onRestart(savedInstanceState);
btn4.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers View is clicked on.
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
String strcount=sharedPreferences.getString("name",scoreText.getText().toString());
if (strcount.equals(""))
{
Toast.makeText(getApplicationContext(), "Data Was Not Found", Toast.LENGTH_SHORT).show();
}
else
{
scoreText.setText(strcount);

}
scoreText.setBackgroundColor(Color.YELLOW);
}
});
}

您使用count作为密钥来保存值

editor.putString("count",scoreText.getText().toString());

但是使用name作为密钥来检索值,所以在获取之前存储的数据时需要使用count密钥,所以使用

sharedPreferences.getString("count",scoreText.getText().toString());

而不是

sharedPreferences.getString("name",scoreText.getText().toString());

您使用不同的键来保存和检索SharedPreferences中的数据。

editor.putString("count",scoreText.getText().toString());
String strcount=sharedPreferences.getString("name",scoreText.getText().toString());

在这两种情况下,你都应该使用相同的键,否则它会返回默认值,即TextView中的文本,并且在应用程序开始时为空,你只需要更改键,这对你来说就很有用了。

只需更改下面的行,就像上面提到的一样

String strcount=sharedPreferences.getString("count",scoreText.getText().toString());

最新更新