1

I am saving my ArrayLists to SharedPreferences in the onPause of my MainActivity. I do this as follows:

TinyDB tinyDB = new TinyDB(activity);

ArrayList<Object> saveUserList = new ArrayList<>();
for (User user : UsersArrayList.getInstance().getUsersList()) {
    saveUserList.add(user);
}

tinyDB.putListObject("userList", saveUserList);

and call the update in my onCreateView as follows:

TinyDB tinyDB = new TinyDB(activity);

ArrayList<User> userList = new ArrayList<>();
for (Object object : tinyDB.getListObject("userList", User.class)) {
    userList.add((User) object);
}
for (User user : userList) {
    if (!UsersArrayList.getInstance().getUsersList().contains(user)) {
        UsersArrayList.getInstance().getUsersList().add(user);
    }
}

The .contains(user) however always returns false and keeps adding the same objects to my the array. And I only want the once that are not in there to be added. Why is the contains operator not working correctly here?

Here is the class with the ArrayList I save the date in and I want updated:

public class UsersArrayList {
    public ArrayList<User> usersList;

    private UsersArrayList() {
        usersList = new ArrayList<User>();
    }

    public ArrayList<User> getUsersList() {
        return usersList;
    }    

    private static UsersArrayList instance;

    public static UsersArrayList getInstance() {
        if (instance == null) instance = new UsersArrayList();
        return instance;
    }
}
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
D.Blazer
  • 192
  • 1
  • 2
  • 12

0 Answers0