Editing SharedPreferences

This is my settings menu, this is a series of 9 image buttons that should change the image depending on the settings. When the image button is clicked, it must change the preference boolean so that the other activity can act accordingly.

It currently populates the image buttons based on the default, but the changes made in the settings menu don't seem permanent.

public class SettingsActivity extends PreferenceActivity {
    ImageButton[] level = new ImageButton[9];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        PreferenceManager.getDefaultSharedPreferences(this).edit().clear().commit();
        PreferenceManager.setDefaultValues(this, R.xml.preferences, true);

        level[0] = (ImageButton) findViewById(R.id.imageButton);
        level[1] = (ImageButton) findViewById(R.id.imageButton2);
        level[2] = (ImageButton) findViewById(R.id.imageButton3);
        level[3] = (ImageButton) findViewById(R.id.imageButton4);
        level[4] = (ImageButton) findViewById(R.id.imageButton5);
        level[5] = (ImageButton) findViewById(R.id.imageButton6);
        level[6] = (ImageButton) findViewById(R.id.imageButton7);
        level[7] = (ImageButton) findViewById(R.id.imageButton8);
        level[8] = (ImageButton) findViewById(R.id.imageButton9);

        PopulateButtons();

    }

            public void PopulateButtons(){
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                for (int i = 0; i < 9; i++) {
                    String prefKey = String.format("lev%s", i);
                    System.out.println(prefKey);
                    if (preferences.getBoolean(prefKey, false) == true) {
                        level[i].setBackgroundResource(R.mipmap.settings_verbs);
                        level[i].setOnClickListener(mySettingsHandler);
                    } else {
                        level[i].setBackgroundResource(R.mipmap.ic_launcher);
                        level[i].setOnClickListener(mySettingsHandler);
                    }

                }
            }


        View.OnClickListener mySettingsHandler = new View.OnClickListener() {
            public void onClick(View v) {
                Context context = getApplicationContext();
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                System.out.println("Running populate method");
                for (int i = 0; i < 9; i++) {
                    if (v.getId() == level[i].getId()) {
                        String prefKey = String.format("lev%s", i);
                        if (preferences.getBoolean(prefKey, false) == false) {
                            System.out.println("Yep");
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putBoolean(prefKey, true);
                            editor.commit();
                            level[i].setBackgroundResource(R.mipmap.settings_verbs);
                        } else {
                            System.out.println("nope");
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putBoolean(prefKey, false);
                            editor.commit();
                            level[i].setBackgroundResource(R.mipmap.ic_launcher);
                        }
                    }
                }
            }
        };

}

      

+3


source to share


1 answer


You set the default values ​​every time call is called. Try



if (savedInstanceState == null) {
    PreferenceManager.getDefaultSharedPreferences(this).edit().clear().commit();
    PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
}

      

0


source







All Articles