Search code examples
androidsharedpreferences

Android Save Multiple Values in SharedPreferences


I have a pretty straightforward toggle button "Like" system. A user id and photo id are sent to the database and stored... then, in the app, I use Shared Preferences to remember if a user has liked a photo or not.

imageToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    addLike();
                    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("liked", imageToggle.isChecked()); // value to store
                    editor.putString("pic_id", foto_id);
                    editor.commit();
                } else {
                    unLike();
                    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean("liked", false); // value to store
                    editor.commit();

                }
            }
        });

Then... whenever the view is created... the preferences are checked to see if the photo is liked or not.

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
        boolean liked = preferences.getBoolean("liked", false);
        String pic = preferences.getString("pic_id", "0");
        final ToggleButton imageToggle = (ToggleButton) findViewById(R.id.like);
        if (pic.equals(foto_id)) {
            imageToggle.setChecked(liked);
        }

The problem... you might have guessed... is that this only works for one photo at a time. If I click on another photo and like it then THAT photo's ID becomes the pic_id and the original photo becomes unchecked.

I'm not even sure SharedPreferences is the right way to achieve what I want to do. (Storing and retrieving multiple values of liked photos). I've looked into everything from saving Sets, to converting everything to JSON... I've thought about querying the database each time to find out if the user has liked a particular photo...

...but I'm SO close to what I want to achieve that I just know there must be some really simple way to do it that just hasn't dawned on me. All I want SharedPreferences to do is remember if I've liked each individual photo or not. Maybe just a new SharedPreference for each photo?

What is the simplest possible solution for what I'm trying to do given what I have so far?


Solution

  • Yes, sharedPreferences should work. You can use fotoID as the key, if you have the fotoID, you can both set and look up the fotoID in shared preferences to manage the boolean "liked".

    imageToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    addLike();
                    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean(foto_id, imageToggle.isChecked()); // changed from "liked". Changed "foto_id" to foto_id -mysticola
                    // remove this line editor.putString("pic_id", foto_id);
                    editor.commit();
                } else {
                    unLike();
                    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
                    SharedPreferences.Editor editor = preferences.edit();
                    editor.putBoolean(foto_id, false); // changed from liked
                    editor.commit();
    
                }
            }
        });
    

    and

       SharedPreferences preferences = getPreferences(MODE_PRIVATE);
            boolean liked = preferences.getBoolean(foto_id, false); // change from "liked"
            // remove String pic = preferences.getString("pic_id", "0");
            final ToggleButton imageToggle = (ToggleButton) findViewById(R.id.like);
            if (liked) { // changed this part
                imageToggle.setChecked(liked);
            }