Unable to change elements in a nested ArrayList

Viewed 125

I have an ArrayList<Menu> in my App, the Menu object looks like:

public class Menu {
    public int id;
    public String desc;
    public boolean opzionale;
    public int limite;
    public List<Prodotti> prodotti;
}

On the user selection i show in my RecyclerView prodotti. each Prodotti on user action can be modified but have to be modified only at the specific position of ArrayList<Menu>.

So initially i just show items at the first position of the Array like:

List<Menu.Prodotti> listProdotti = listMenu.get(fase).prodotti;

fase is a dynamic value which specify which items of which Menu has to be shown.

Then in another function i change the value of the Prodotto at position like:

    Menu.Prodotti prodotto = listProdotti.get(position);
    prodotto.setQta(qtaProdotto + qta);

The issue is that after doing so all prodotti of ArrayList<Menu> at position will update it's quantity.

So if in my array i have data like:

ArrayList<Menu.Prodotti> prodotti = new ArrayList<>();
prodotti.add(new Menu.Prodotti("TEST1", "1", 0, 0, 1, 0));
prodotti.add(new Menu.Prodotti("TEST2", "2", 0, 0, 1, 0));
prodotti.add(new Menu.Prodotti("TEST3", "3", 0, 0, 1, 0));
prodotti.add(new Menu.Prodotti("TEST4", "4", 0, 0, 1, 0));
prodotti.add(new Menu.Prodotti("TEST5", "5", 0, 0, 1, 0));

ArrayList<Menu> listMenu = new ArrayList<>();
listMenu.add(new Menu(1, "Fase 1", false, 1, prodotti));
listMenu.add(new Menu(2, "Fase 2", false, 1, prodotti));
listMenu.add(new Menu(3, "Fase 3", false, 1, prodotti));
listMenu.add(new Menu(4, "Fase 4", true, 2, prodotti));

And i change the listMenu.get(0).prodotti.get(1).setQta(2) ALL the products in the listMenu at position 1 will be set with Qta 2.

All my logics are in RecyclerView.Adapter.

1 Answers

Here is a simple sketch what your test-case looks like.

(The outer boxes are the ArrayList and the inner boxes are the objects of Menu and Prodotti)

Sketch of the testcase in question

So if you change Prodotti 1 from Menu 1, you are also changing Prodotti 1 from Menu 2, because they are referring to the same List and therefore also to the same Prodotti-Object.

Try it with a testcase like this:

  ArrayList<Menu.Prodotti> prodottiList1 = new ArrayList<>();
  prodotti.add(new Menu.Prodotti("TEST1", "1", 0, 0, 1, 0));
  prodotti.add(new Menu.Prodotti("TEST2", "2", 0, 0, 1, 0));
  prodotti.add(new Menu.Prodotti("TEST3", "3", 0, 0, 1, 0));

  ArrayList<Menu.Prodotti> prodottiList2 = new ArrayList<>();
  prodotti.add(new Menu.Prodotti("TEST4", "4", 0, 0, 1, 0));
  prodotti.add(new Menu.Prodotti("TEST5", "5", 0, 0, 1, 0));
  prodotti.add(new Menu.Prodotti("TEST6", "6", 0, 0, 1, 0));


  ArrayList<Menu> listMenu = new ArrayList<>();
  listMenu.add(new Menu(1, "Fase 1", false, 1, prodottiList1));
  listMenu.add(new Menu(2, "Fase 2", false, 1, prodottiList2));

Here both Menu objects are referring to two different Lists, which are containing different objects.

Here another sketch what the objects in the code above look like :

Sketch of the testcase in answer

Related