General preferences are saved only for the first time

I had 3 days with this problem and is killing me. The program creates the settings for the first time, but after that it never changes them.

This is PreferencesScreen where 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 settings, I have ListPreference and Preference, which cause 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 is here. Work problems are 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();
    }
}

}

, , , . , , , , , , ( ). , . , ListPreference, , .

, , , . .

+5
1

, , . , , .

, , . , , , . :

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();      
}
+14

All Articles