2012-09-21 77 views
5

我已經3天了這個問題,正在殺我。 該程序首次創建首選項,但之後永遠不會更改它們。共享首選項只保存第一次

這是調用xml的PreferencesScreen。

public class PreferencesScreen extends PreferenceFragment{ 

private final String TAG = "PreferencesScreen"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.d(TAG, "OnCreate"); 
    addPreferencesFromResource(R.xml.prefs); 
} 

在首選項中,我有一個ListPreference和一個首選項,它調用存儲電子郵件的活動。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

<PreferenceCategory android:title="Information Collected"> 
    <ListPreference 
     android:key="loggins" 
     android:title="Logs Stored" 
     android:summary="Choose the top kind of logs do you want to store." 
     android:dialogTitle="Choose Logs" 
     android:entries="@array/logs" 
     android:entryValues="@array/logsValues"/> 
</PreferenceCategory> 

<PreferenceCategory android:title="Email Configurations"> 
     <Preference 
       android:key="pushing" 
       android:title="The Email Activity" 
       android:summary="Just push"> 
      <intent android:action = "ADDING_EMAIL"/> 
     </Preference> 
</PreferenceCategory> 
</PreferenceScreen> 

一切都在這裏。該問題在所謂的活動......

public class AddingEmail extends ListActivity implements OnClickListener{  

private Set<String> emails; 
private EditText emailAdd; 
SharedPreferences.Editor editor; 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.addingemail); 
    Button add = (Button) findViewById(R.id.add); 
    emailAdd = (EditText) findViewById(R.id.email); 
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
    editor = prefs.edit(); 

    prefList = toArrayList(prefs.getStringSet("emailWrongs", null)); 
    add.setOnClickListener(this); 
} 


public void onClick(View v) { 
    Set<String> list = prefs.getStringSet("emailWrongs", null); 
    String newEmail = emailAdd.getText().toString();   
    if (list==null){ //first time the preferences are called. 
     emails = new TreeSet<String>(); 
     editor.putStringSet("emailWrongs", emails); 
     editor.apply(); 
    } 
    if (newEmail != ""){ 
     emails=prefs.getStringSet("emailWrongs", null); 
     emails.add(newEmail); 
     editor.putStringSet("emailWrongs", emails); 
     editor.apply(); 
    } 
} 

} 

的一點是,它總是存儲在第一時間很好,但如果我當我添加其他電子郵件偏好doesnt't變化。他們看起來像他們改變,因爲如果我打印他們,他們會顯示我添加的所有電子郵件,但首選項文件不會更改(在文件資源管理器中檢查它)。如果我重新啓動或關閉並再次打開,首選項僅與我添加的第一封電子郵件。 事情是如果我回到並更改ListPreference的首選項,那麼它會存儲所有更改,甚至是我添加的電子郵件。

希望我很清楚,它有很多代碼,因爲我想要非常明確。 謝謝你的幫助。

+0

我試着改變prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); prefShared = getApplicationContext()。getSharedPreferences(「com.example.daemon3_preferences」,MODE_PRIVATE); 也有同樣的結果。 – Alberto

回答

14

經過一個多星期的尋找我發現它的錯誤。 我認爲這可以幫助很多有同樣問題的人。

問題是,當我調用首選項來獲取字符串集時,它只引用列表並且不做它的副本。因此,我必須創建一個新列表並添加之前存儲的所有元素,並添加新元素,然後使用編輯器更改新列表的首選項。 代碼是這樣的:

Set<String> list = prefs.getStringSet("emailWrongs", null); 
Set<String> newList = new TreeSet<String>(); 
String newEmail = emailAdd.getText().toString();   
if (newEmail != ""){ 
    if (list != null){ 
     for(String each: list){ 
      newList.add(each); 
     } 
    } 
    newList.add(newEmail); 
    editor.putStringSet("emailWrongs", newList);  
    editor.apply();  
} 
+1

我做了什麼就像'Set list = new HashSet (prefs.getStringSet(「emailWrongs」,null);'然後只是當你做了'list'和'putStringSet(...' – RTF

+0

你是救命恩人 – hellowill89

+0

在這個問題上浪費了兩天,謝謝 –