I have a list view those will loaded from shared preference. when user clicks on delete, it should remove from list and shred preferences. My problem is, the particular item is not deleted from shared preferences, for this i used below code,
SharedPreferences settings = getSharedPreferences(
OptionsActivity.PREFS_NAME, 0);
int deviceSize = settings.getInt("deviceSize", 0);
SharedPreferences.Editor editor = settings.edit();
String deviceName = items.get(index);
editor.remove(deviceName + String.valueOf(items.get(index)));
editor.remove("deviceName" + String.valueOf(index));
int deviceid = settings.getInt("deviceId"+String.valueOf(items.get(index)),0);
editor.remove("deviceId" + String.valueOf(deviceid));
editor.putInt("deviceSize", deviceSize - 1);
editor.commit();
By this code only deviceSize-1 is working, so it seem to be deleted from list but when i see the shared Preferences file, its not.
My shared Preferences file is below,
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="deviceName1">APPLE</string>//delete when click on delete
<int name="deviceId1" value="0" /> //delete when click on delete
<int name="deviceSize" value="1" /> //decrease by one when item deleted
</map>
when user clicks on delete i want to delete these 3 lines form preference file. How to delete the whole row?The list shows only name(APPLE).
There is mistake on line :
it should be
Maybe you also want to remove the deviceSize key :
before commiting.
Your code is not very clear. Make it more readable.