2014-11-24 79 views
0

我正在制作一个包含表单的应用程序,并且每当一个数据点击一个按钮从EditText中的BD加载时,但每次按下其他按钮时,其他EditText都会被清除,我试着用:变量需要保存

@Override 
protected void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    savedInstanceState.putString("data", myVariable); 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    other = savedInstanceState.getString("data"); 
    name.setText(other); 

}

很抱歉,如果我没有解释好,我需要他们每次更改活动变量和我没有删除。有什么建议么?谢谢!

回答

0

尝试使用Android SharedPreferences代替。这是Android为其应用提供的持久性键值存储,旨在涵盖这类问题。以下是使用它的简单方法:

SharedPreference prefs = PreferenceManager.getDefaultSharedPreferences(this); 

// To put a data (in this case a String from your EditText).. 
Editor editor = prefs.edit(); 
editor.putString("data", yourStringHere); 
editor.commit(); 

... 

// ..and to retrieve it.. 
prefs.getString("data", null); 

// NOTE: The 'null' in above method call could be replaced by any String you want; 
// it basically specifies a default value to get when "data" is empty or doesn't 
// yet exist. 

希望这会有所帮助。

+0

非常感谢你,我发现它非常有帮助 – 2014-11-25 14:52:38

+0

很高兴我能提供帮助。 :) – ridsatrio 2014-11-25 14:56:31