samedi 5 mars 2016

SharedPreferances not working for some checkboxes but only for few

I have a problem with checkBoxed in SharedPreferances in my application. I use the exact same code for five checkboxes. One checkbox is working fine. Now, the three from the other four checkboxes are going to be like the fourth checkbox. What i mean is that when i check the fourth checkbox and i save the preferances tha other 3 checkboxes are going the same as the fourth. Here is the code where the 'fourth' checkbox is the sound and the checkbox that is working fine is the recommended post.

SaveProfileState class

public class SaveProfileState {

    private final static String THE_RATIO="10";
    private final static String TEXT_POSTS="true";
    private final static String EVENT_POSTS="true";
    private final static String PHOTO_POSTS="true";
    private final static String SOUND="true";
    private final static String RECOMMENDED_POSTS="false";
    private SharedPreferences userPrefs;// save data on the device
    private static final String PREFS_NAME="userProfileState2";//definition for shared

    public SaveProfileState(Context context){
        userPrefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }
    public SaveProfileState(){
    }

    public void saveChangesState(int ratio, boolean textPosts, boolean eventPosts, boolean photoPosts, boolean sound, boolean recommendedPosts){
        SharedPreferences.Editor editor = userPrefs.edit();
        editor.putInt(THE_RATIO, ratio);
        editor.putBoolean(TEXT_POSTS, textPosts);
        editor.putBoolean(EVENT_POSTS, eventPosts);
        editor.putBoolean(PHOTO_POSTS, photoPosts);
        editor.putBoolean(SOUND, sound);
        editor.putBoolean(RECOMMENDED_POSTS, recommendedPosts);


        editor.commit();
    }

    public int getRatio(){
        return userPrefs.getInt(THE_RATIO, 0);
    }

    public boolean getTextPostsCheck(){
        return userPrefs.getBoolean(TEXT_POSTS, true);
    }

    public boolean getEventPostsCheck(){
        return userPrefs.getBoolean(EVENT_POSTS, true);
    }

    public boolean getPhotoPostsCheck(){
        return userPrefs.getBoolean(PHOTO_POSTS, true);
    }

    public boolean getSoundCheck(){
        return userPrefs.getBoolean(SOUND, true);
    }

    public boolean getRecommendedPostsCheck(){
        return userPrefs.getBoolean(RECOMMENDED_POSTS, false);
    }
}

Profile class

 public class Profile extends AppCompatActivity {
        private SeekBar seekBar;
        private TextView textView;
        private CheckBox textPosts;
        private CheckBox eventPosts;
        private CheckBox photoPosts;
        private CheckBox sounds;
        private CheckBox recommendedPosts;
        private boolean textPost;
        private boolean eventPost;
        private boolean photoPost;
        private boolean sound;
        private boolean recommendedPost;

        private void initializeVariables() {
            seekBar = (SeekBar) findViewById(R.id.seekBar1);
            textView = (TextView) findViewById(R.id.textView1);
            textPosts = (CheckBox) findViewById(R.id.textPostsOption);
            eventPosts = (CheckBox) findViewById(R.id.eventPostsOption);
            photoPosts = (CheckBox) findViewById(R.id.photoPostsOption);
            sounds = (CheckBox) findViewById(R.id.soundOption);
            recommendedPosts = (CheckBox) findViewById(R.id.recommendedBox);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_profile);
            initializeVariables();
            SaveProfileState sps = new SaveProfileState(this);
            textPost = sps.getTextPostsCheck();
            textPosts.setChecked(sps.getTextPostsCheck());
            eventPosts.setChecked(sps.getEventPostsCheck());
            sounds.setChecked(sps.getSoundCheck());
            photoPosts.setChecked(sps.getPhotoPostsCheck());
            recommendedPosts.setChecked(sps.getRecommendedPostsCheck());

            final Button button = (Button) findViewById(R.id.saveChanges);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    saveChanges();
                }
            });


        }       

        public void saveChanges(){
            SaveProfileState sps = new SaveProfileState(this);
            sps.saveChangesState(ratio, textPosts.isChecked(), eventPosts.isChecked(), photoPosts.isChecked(), sounds.isChecked(), recommendedPosts.isChecked());
        }      
    }

XML for class Profile

<ScrollView xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:facebook="http://ift.tt/LrGmb4"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="none"
    android:background="@color/blue">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="View only recommended Posts"
            android:id="@+id/recommendedBox"
            android:layout_below="@+id/textView1"
            android:layout_alignParentStart="true"
            android:checked="false" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text Posts"
            android:id="@+id/textPostsOption"
            android:layout_below="@+id/postKinds"
            android:layout_alignStart="@+id/textView1"
            android:checked="true" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Event Posts"
            android:id="@+id/eventPostsOption"
            android:layout_below="@+id/textPostsOption"
            android:layout_alignStart="@+id/textPostsOption"
            android:checked="true" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Photo Posts"
            android:id="@+id/photoPostsOption"
            android:layout_below="@+id/eventPostsOption"
            android:layout_alignStart="@+id/eventPostsOption"
            android:checked="true" />
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sound"
            android:id="@+id/soundOption"
            android:layout_below="@+id/photoPostsOption"
            android:layout_alignParentStart="true"
            android:checked="true" />


        <Button
            android:text="Save Changes"
            android:id="@+id/saveChanges"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center"
            android:background="@drawable/go_btn"
            android:padding="6dp"
            android:textSize="13dp"
            android:layout_marginTop="5dp"
            android:layout_below="@+id/login_button"

            />

Here is a photo of what it looks like App I cannot find the problem and i find it very strange because the same code works for one checkbox and not for the others..

Thanks!




Aucun commentaire:

Enregistrer un commentaire