Tôi đã có 3 ngày với vấn đề này và đang giết tôi. Chương trình tạo tùy chọn lần đầu tiên nhưng sau đó chương trình không bao giờ thay đổi chúng.Tùy chọn được chia sẻ chỉ lưu lần đầu tiên
Đây là PreferencesScreen nơi xml được gọi.
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);
}
Trong tùy chọn, tôi có ListPreference và Tùy chọn gọi hoạt động để lưu trữ email.
<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>
Mọi thứ cho đến đây. Các sự cố có trong hoạt động được gọi là ...
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();
}
}
}
Vấn đề là luôn luôn lưu trữ lần đầu tiên nhưng nếu tôi thêm email khác thì tùy chọn sẽ không thay đổi. Họ có vẻ như họ thay đổi bởi vì nếu tôi in chúng, họ hiển thị tất cả các email tôi thêm nhưng tệp tùy chọn không thay đổi (Kiểm tra nó trong File Explorer). Và nếu tôi khởi động lại hoặc đóng và mở lại, các ưu tiên chỉ với email đầu tiên tôi thêm vào. Vấn đề là nếu tôi quay trở lại và thay đổi sở thích của ListPreference, sau đó nó lưu trữ tất cả các thay đổi ngay cả những email tôi đã thêm vào.
Hy vọng tôi đã rõ ràng, nó có rất nhiều mã vì tôi muốn rất rõ ràng. Cảm ơn sự giúp đỡ của bạn.
Tôi đã thử thay đổi prefs = PreferenceManager.getDefaultSharedPreferences (getApplicationContext()); cho prefShared = getApplicationContext(). getSharedPreferences ("com.example.daemon3_preferences", MODE_PRIVATE); với cùng kết quả. – Alberto