我正在尝试保存String lolzor,但是我似乎无法让它重新出现,一旦应用程序被销毁并重新启动,我尝试了多种解决方案但是它们似乎对我有效可以任何人帮助?
提前致谢
感谢爱琴海,我找到了解决方案。问题既不是保存或获取信息,而是事实上,一旦我检索到保存的信息,我没有使用它。
代码如下所示:
public class MainActivity extends Activity {
public static final String GEMFFS = "dk.SimonPedersen.GEMFFS";
SharedPreferences ada;
SharedPreferences.Editor editor;
Button btnGem;
ToggleButton tbKon;
EditText etVaegt;
Double sex, vaegt, KT;
String lols, lolzor, KTO;
TextView haha;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ada = getSharedPreferences(GEMFFS, 0);
haha=(TextView)findViewById (R.id.haha);
btnGem = (Button) findViewById (R.id.btnGem);
tbKon = (ToggleButton) findViewById (R.id.tbKon);
etVaegt = (EditText) findViewById (R.id.etVaegt);
tbKon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
sex=0.7d;
} else {
sex=0.6d;
}
Log.d("Konskonstant", "sex" + sex);
}
});
btnGem.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
lols=etVaegt.getText().toString();
vaegt=Double.parseDouble(lols);
KT = sex*vaegt;
lolzor=KT.toString();
haha.setText("KT " + KT);
}
});
}
protected void onStart(){
super.onStart();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
String KT = settings.getString("KT", null);
}
protected void onDestroy(){
super.onDestroy();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("KT", lolzor);
editor.commit();
};
}
答案 0 :(得分:1)
改变这个:
protected void onStart(){
super.onStart();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
String KT = settings.getString("KT", null);
}
对此:
protected void onStart(){
super.onStart();
SharedPreferences settings = getApplicationContext().getSharedPreferences(GEMFFS, 0);
KTO = settings.getString("KT", null);
KT = Double.valueOf(KTO);
}
同时将此值设置为您想要的值(对任何TextView
)
注意:您在保存和获取值方面没有任何问题。唯一的问题是,当你得到它时,你没有使用它。