mercredi 25 mai 2016

getDefaultSharedPrefereneces returns false for all booleans when putting multiple booleans

I'm using DefaultSharedPrefeneces to save and load the state of the checkboxes in my navigation drawer.

when the navigation drawer is created and the state of the checkboxes are loaded from the DefaultPreferences, if the booleans for each checkbox are true then the checkboxes are set to checked.

when the user clicks on a checkbox the state then get saved to the defaultsharedPrefences. I have no problem with all this.

the problem is that then i try to put 2 booleans into the defaultsharedprefences before the commit it doesn't work and appears to overwrite the first boolean.

Here is my code

//Navigation Drawer
private void addDrawerItems() {


    String[] osArray = {"item1", "item2", "item3"};

    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);


    if (mIsPremiumUser) {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    } else {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
    }

    mDrawerList.setAdapter(mAdapter);


    Boolean isCheckedValue;


    // *** THIS IS WHERE I LOAD THE CHECKBOX STATE FROM DEFAULTSHAREDPREFERENCES ***
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    isCheckedValue = preferences.getBoolean("cbox1", false);
    mDrawerList.setItemChecked(0, isCheckedValue);

    isCheckedValue = preferences.getBoolean("cbox2", true);
    mDrawerList.setItemChecked(1, isCheckedValue);

    isCheckedValue = preferences.getBoolean("cbox3", true);
    mDrawerList.setItemChecked(2, isCheckedValue);



    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            CheckedTextView ctv = (CheckedTextView) view;

            if (!mIsPremiumUser) {
                Toast.makeText(getApplication(), "Upgrade", Toast.LENGTH_LONG).show();
                return;
            }


            switch (position) {

                case 0:


                    if (ctv.isChecked()) {

                        requestActivityUpdates();
                        Toast.makeText(getApplicationContext(), "item1ON", Toast.LENGTH_SHORT).show();

                        // THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();

                        editor.putBoolean("cbox1", ctv.isChecked());
                        editor.commit();


                    } else {

                        removeActivityUpdates();
                        Toast.makeText(getApplicationContext(), "item1OFF", Toast.LENGTH_SHORT).show();


                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();

                        editor.putBoolean("cbox1", false);
                        editor.commit();


                    }

                    break;

                case 1:
                    if (ctv.isChecked()) {

                        Toast.makeText(getApplicationContext(), "item2 ON", Toast.LENGTH_LONG).show();


                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();

                        // THIS IS WHERE I SAVE THE STATE OF THE CHECKBOX IN DEFAULT SHAREDPREFENCES
                        editor.putBoolean("cbox2", ctv.isChecked());
                        editor.putBoolean("callStatus", ctv.isChecked()); // << THIS LINE IS CAUSING ME PROBLEMS 
                        editor.commit();


                    } else {

                        Toast.makeText(getApplicationContext(), "item2 OFF", Toast.LENGTH_LONG).show();


                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();

                        editor.putBoolean("cbox2", ctv.isChecked());
                        editor.putBoolean("callStatus", ctv.isChecked());
                        editor.commit();


                    }
                    break;

                case 2:
                    if (ctv.isChecked()) {

                        Toast.makeText(getApplicationContext(), "item3 ON", Toast.LENGTH_LONG).show();


                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();

                        editor.putBoolean("cbox3", ctv.isChecked());
                        editor.putBoolean("smsStatus", ctv.isChecked());
                        editor.commit();


                    } else {

                        Toast.makeText(getApplicationContext(), "item3 OFF", Toast.LENGTH_LONG).show();


                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = preferences.edit();

                        editor.putBoolean("cbox3", ctv.isChecked());
                        editor.putBoolean("smsStatus", ctv.isChecked());
                        editor.commit();

                    }
                    break;

            }


        }
    });
}

I know for sure that it works for one putBoolean on each item clicked, but doesn't when I've two putBoolean within the same if/else before a editor.commit . any suggestions on whats causing the problem and how to fix it ? would be greatly appreciated it.




Aucun commentaire:

Enregistrer un commentaire