2014-11-24 66 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