我已經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的首選項,那麼它會存儲所有更改,甚至是我添加的電子郵件。
希望我很清楚,它有很多代碼,因爲我想要非常明確。 謝謝你的幫助。
我試着改變prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); prefShared = getApplicationContext()。getSharedPreferences(「com.example.daemon3_preferences」,MODE_PRIVATE); 也有同樣的結果。 – Alberto