ArrayList.remove(i) doesn't delete all objects as it should

Viewed 2022

I have a simple listview(recyclerView), where each list-item have a checkbox. All items that is checked should be deleted with this code:

for(int i = 0; i<shopListItems.size(); i++) {
    if(shopListItems.get(i).isBought == 1) {
        shopListItems.remove(i);
    }
}

But for some reason it doesn't. If 10 items are checked, then only 7 of them will be deleted and I have to run the code again to delete the rest.

The code seems fine, but I really don't understand why it doesn't delete all of the checked, or all that are (isBought == 1).

3 Answers
Related