The program creates preferences the first time but after that it never changes them. I would appreciate assistance in understanding why.
This is the PreferencesScreen where the xml is called.
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);
}
In the preferences I have a ListPreference and a Preference which calls an activity to store emails.
<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>
Everything until here. The problems are in the activity called...
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();
}
}
}
The point is that it always stores the first time well but if I when I add another email the preferences doesnt't change. They looks like they change because if I printed them they show all the emails I add but the preference file doesn't change (Checking it in the File Explorer). And if i reboot or close and open again, the preferences are only with the first email I add. The thing is if i back to and change the preference of the ListPreference, then it stores all the changes even the emails I added.
Hope I was clear, it has a lot of code because i wanted to be very explicit. Thank you for the help.
Yes you can maintain as many shared preference files for an app as you can.
There's no hard limit. The main reason it is not recommended to use SharedPreferences in place of the database is mainly the performance -> shared preferences data is keept in ordinary flat XML file which lacks all the features SQLite offers.
After more than a week looking for the mistake I found it. I think this can be helpful for a lot of people who had the same trouble.
The problem was that when I call the preferences to get the String Set, it only reference the list and not make a copy of it. So I have to create a new list and add all the elements stored before and also add the new element and then with the editor change the preferences with the new list. The code is like this:
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With