2

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?

mystic cola
  • 1,465
  • 1
  • 21
  • 39

3 Answers3

1

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);
        } 
mystic cola
  • 1,465
  • 1
  • 21
  • 39
Jeff Padgett
  • 2,380
  • 22
  • 34
  • We have a winner! You made one very tiny mistake: You put: editor.putBoolean("foto_id", imageToggle.isChecked()); ......instead of: editor.putBoolean(foto_id, imageToggle.isChecked()); ||||||| Already fixed it. Thanks! I *knew* I was close! :) – mystic cola Jun 15 '18 at 18:10
1

try this:

 var buttonLocalState: SharedPreferences =
    context.getSharedPreferences(SPP_NAME, MODE_PRIVATE)

 fun savebuttonstate(mExampleList: ArrayList<commentItemModel>) {
    val userLocalDatabaseEditor: SharedPreferences.Editor = buttonLocalState.edit()
    val gson = Gson()
    val json = gson.toJson(mExampleList)
    userLocalDatabaseEditor.putString("yesbtn list", json)
    userLocalDatabaseEditor.apply()
}


fun getyesBtn(): ArrayList<commentItemModel> {
    val gson = Gson()
    var mExampleList: ArrayList<commentItemModel> = ArrayList<commentItemModel>()
    val json: String = buttonLocalState.getString("yesbtn list", null).toString()
    val type: Type = object : TypeToken<ArrayList<commentItemModel?>?>() {}.getType()

    if (!mExampleList.isEmpty()) {
        mExampleList = gson.fromJson<Any>(json, type) as ArrayList<commentItemModel>
    }
    return mExampleList
}

this is model class to add item to list for example: list.Add(commentItemModel(1, true))

data class commentItemModel(var id: Int, var clicked: Boolean) {

}

Hassan
  • 380
  • 6
  • 20
0

Because you don't know how many photos will be selected and it can change quite dynamically, you might want to save a list of like photo ids into shared preferences. Normally shared preferences only allow you saving sets of data not lists so this SO question shows how to do that.

Save ArrayList to SharedPreferences

psydj1
  • 181
  • 10
  • I already mentioned the Sets as being a more complicated solution to a much simpler problem. Jeff's answer is correct: All I had to do was use the foto_id as a key. This is actually what SavedPreferences was designed to do: Item1, true Item2, false Item 3, false... etc. Now each foto_id is an item with its own boolean. That's really all I wanted. Arrays... and lists... and Sets are just overkill. – mystic cola Jun 15 '18 at 18:16