0

I am using shared preferences to store different languages a user enters and display them in a recyclerView . when the users clicks the remove button , i want to remove the selected value from the shared pref

I have written the following code but i don't know where i went wrong

 pref = contexts.getSharedPreferences("user", 0);
 gson = new Gson();
 json = pref.getString("language" , "");
 Type type = new TypeToken<List<String>>() {}.getType();
 List<String> DataPackage = gson.fromJson(json, type);
 String item = DataPackage.get(getAdapterPosition());
 String items  =  pref.getString("language" , item);
 editor = pref.edit();
 editor.remove(items);
 editor.commit();

I want the single item from the pref to be removed but nothing happens , it dosn't remove the data

Spiderman
  • 3
  • 3

1 Answers1

0

After removing the item from list , Save the current in sharedPref with same key . This will replace the previous list with current .

And try to use "apply" instead of "commit" when applying any changes .This will update async.

For further you may check the following post : Remove one object from Arraylist<object> saved in sharedPreferences

Mini Chip
  • 949
  • 10
  • 12