2013-05-17 73 views
0

所以這是我first.javaSharedPreference沖洗或清除不工作

SharedPreferences preferences = getSharedPreferences("myPrefss", getApplicationContext().MODE_PRIVATE); 
preferences.getString(currentPlace, ""); 
SharedPreferences.Editor editor = preferences.edit(); 
editor.putString("CurrentPlace", currentPlace); 
editor.commit(); 

這是我second.java

SharedPreferences preferences = getSharedPreferences("myPrefss", getApplicationContext().MODE_PRIVATE);  
String curlocation; 
curlocation = preferences.getString("CurrentPlace",""); 

說,第一類爲輸出爲aaa第一次現在當我回去並重做這個過程時,我得到aaaaaa,字符串被追加。任何想法如何我可以避免這一點?

回答

0

使用preferences.clear()SharedPreferences.Editor editor = preferences.edit();

SharedPreferences preferences = getSharedPreferences("myPrefss", getApplicationContext().MODE_PRIVATE); 
SharedPreferences.Editor editor = preferences.edit(); 
editor.clear(); 
editor.putString("CurrentPlace", currentPlace); 
editor.commit(); 
+0

即時得到此 - >明確()的方法,是未定義的類型SharedPreferences – jayeshkv

+0

'SharedPreferences偏好= getSharedPreferences(「myPrefss 「,getApplicationContext()。MODE_PRIVATE);' 'SharedPreferences.Editor editor = preferences.edit(); editor.clear(); editor.putString(「CurrentPlace」,currentPlace); editor.commit();'仍然我得到附加值 – jayeshkv

+0

第一次我得到1112和第二次我得到11121112.這是目前的代碼是如何工作的。 – jayeshkv

0

嘗試使用

SharedPreferences preferences = getSharedPreferences("myPrefss", getApplicationContext().MODE_PRIVATE); 
//preferences.getString(currentPlace, ""); // Remove this line 
SharedPreferences.Editor editor = preferences.edit(); 
editor.putString("CurrentPlace", currentPlace); 
editor.commit(); 
+0

已經刪除了 – jayeshkv