Deleting an item (index) from SharedPreferences

Tags: , , ,



I’m saving an array called semesterArray which is a List<String> semesterArray = new ArrayList<>() with the following method on SharedPreferences:

public void saveSemesterArray() {
    StringBuilder stringBuilder = new StringBuilder();

    for(String str: semesterArray) {
        stringBuilder.append(str);
        stringBuilder.append(",");
    }

    SharedPreferences currentSharedPreferences = getSharedPreferences("CURRENT", MODE_PRIVATE);
    SharedPreferences.Editor editor = currentSharedPreferences.edit();
    editor.putString("currentSemesterArray", stringBuilder.toString());

    if(!currentSharedPreferences.getString("currentSemesterArray", "").equals(
            currentSharedPreferences.getString("updatedSemesterArray", ""))) {
        editor.putString("updatedSemesterArray", currentSharedPreferences.getString(
                "currentSemesterArray", "") + stringBuilder.toString());
        editor.apply();
    }
    editor.apply();
}

The if statement is there to update the SharedPreferences, I got the idea from this StackOverflow answer. Now, the following method takes care of loading the SharedPreferences that were just saved:

public void loadSemesterArray() {
    SharedPreferences sharedPreferences = getSharedPreferences("CURRENT", MODE_PRIVATE);
    String semestersString = sharedPreferences.getString("updatedSemesterArray", "");
    String[] itemsSemesters = semestersString.split(",");
    List<String> items = new ArrayList<String>();
    noDuplicates = new ArrayList<String>();

    for(int i = 0; i < itemsSemesters.length; i++) {
        items.add(itemsSemesters[i]);
    }

    // Removing duplicates from Semesters list
    for(String str: items) {
        if(!noDuplicates.contains(str)) {
            noDuplicates.add(str);
        }
    }

    for(int i = 0; i < noDuplicates.size(); i++) {
        Log.d("NoDuplicatesList", noDuplicates.get(i));
    }
}

We remove duplicate Semesters because the if statement in the saveSemesterArray method duplicates the already saved Semesters into the updated SharedPreferences. Now, I want to create a a method that deletes an item (index) from the SharedPreferences. For example, if I have Spring 2020, Summer 2020, and Fall 2020, I’d be able to delete any index in that list and save it again so the list is updated. Is there a way this can be done using SharedPreferences? If not what should I be using instead? I appreciate any help.

Answer

First, you need to remember you’re not saving a list into SharedPreferences, you’re saving a string that serlizes the data in a format that’s easy to deserlize.

Second, saving a string into SharedPreferences is a very quick task, you don’t really need to make an if check to make sure they’re not exactly the same.

Third, the only way to delete an item from that serlized list would be :

1- Deserlizing the list saved.

2- Removing the item at the index.

3- Reserlizing the list and saving it again.



Source: stackoverflow