androidarraylistsharedpreferencesnavigationviewmenu-items

I can't delete an item from sharedpreferences


I have a list of item in navigationview witch are added programmatically - and I save and restore them using sharedpreferences, everything works fine.But the problem is that I can't delete an item from sharedpreferences.I retrieve the item index using the title of the current item clicked - the code works fine - the item position is retrieved correctly but is not deleted from shared preferences, nothing works.What can I do to solve this problem?

This is my code:

    private void saveData() {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_LIST_KEY, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(navItems);
        editor.putString(SHARED_PREF_LIST_KEY_ITEM, json);
        editor.apply();
    }

    private void loadData() {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_LIST_KEY, MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString(SHARED_PREF_LIST_KEY_ITEM, null);
        Type type = new TypeToken<ArrayList<NavItem>>() {
        }.getType();
        navItems = gson.fromJson(json, type);
        if (navItems == null) {
            navItems = new ArrayList<>();
        }
    }

    private void deleteData(NavItem navItem) {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_LIST_KEY, MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString(SHARED_PREF_LIST_KEY_ITEM, null);
        Type type = new TypeToken<ArrayList<NavItem>>() {
        }.getType();
        navItems = gson.fromJson(json, type);
        if (navItems != null) {
            navItems.remove(navItem);
                        Toast.makeText(this, "List " + navItem.getName() + " 
                                          deleted ", Toast.LENGTH_SHORT).show();

            saveData(); // save updated data to shared preferences
        }
    }




           int position = navViewArray.findItemString(checkItemTitle);
                                Toast.makeText(this, "position " + position, 
                                                 Toast.LENGTH_SHORT).show();

               NavItem navItem = navItems.get(position);
                deleteData(navItem);



     public class NavItem {
       private String name;

       public NavItem(String name) {
        this.name = name;
    }

       public String getName() {
        return name;
    }
}

     public class NavViewArray {
       private List<NavItem> navItems;

       public NavViewArray(List<NavItem> items) {
        this.navItems = new ArrayList<>(items);
    }

       public int findItemString(String itemName) {
        for (int i = 0; i < navItems.size(); i++) {
            NavItem navItem = navItems.get(i);
            if (itemName.equals(navItem.getName())) {
                return i;
            }
        }
        return -1;
    }

}

Solution

  • To be able to find the item in the List using the item itself, you need to implement equals and hashcode.

    For this line of code to work navItems.remove(navItem); You need the NavItem class to override equals and hashcode

    i.e. here:

    public class NavItem {
       private String name;
    
       public NavItem(String name) {
          this.name = name;
       }
    
       public String getName() {
          return name;
       }
    
       // ADD THE BELOW
    
       @Override
       public boolean equals(NavItem other) {
            return name.equals(other.name);
       }
    
       @Override
       public int hashcode() {
           return name.hashcode();
       }
    
    }
    

    This is a very simple equals and hashcode, you can use the IDE to generate your equals and hashcode which will do a better job of taking all fields into account.

    https://www.jetbrains.com/help/idea/generate-equals-and-hashcode-wizard.html


    You can also validate that the item has been removed, as the removed item is returned:

    NavItem removed= navItems.remove(navItem);
    Toast.makeText(this, "List " + removed.getName() + "deleted ",b Toast.LENGTH_SHORT).show();