This makes the RecyclerView blank:

      public void onTicketsFetched(SupportTicketModel[] fetchedTickets) {
            ArrayList<SupportTicketModel> ticketModelsUpdated = new ArrayList<>(Arrays.asList(fetchedTickets));
            ticketModels.clear();
            ticketModels = ticketModelsUpdated;
            ticketAdapter.notifyDataSetChanged();
      }

Whereas this works:

     public void onTicketsFetched(SupportTicketModel[] fetchedTickets) {
            ArrayList<SupportTicketModel> ticketModelsUpdated = new ArrayList<>(Arrays.asList(fetchedTickets));
            ticketModels.clear();
            ticketModels.addAll(ticketModelsUpdated);
            ticketAdapter.notifyDataSetChanged();
     }

DIFFERENCE:

  • First: ticketModels = ticketModelsUpdated;

  • Second: ticketModels.addAll(ticketModelsUpdated);

As far as I have understood, both ticketModels and ticketModelsUpdated when done ticketModels = ticketModelsUpdated will essentially refer to the same list. So technically, ticketModels should have every value of ticketModelsUpdated. But this is giving me error. Any idea ?

I understand ticketMOdels.AddAll(ticketModelsUpdated) will create two lists of same value. And it is working fine.

So whats up ?

4 Answers

0
Amir jodat On

in Java, lists work by reference. In the first part, you are changing both the memory addresses and values of your items, while, in the second part, you are changing the values only. that's why your second solution works.

0
Community On

The issue could be in your adapter

Assuming you pass ticketModels to your adapter and you save this reference in a list variable.

When you do

ticketModels.addAll(ticketModelsUpdated);

the internal adapter list pointer is always referencing ticketModels. So, it works.

But with this:

ticketModels = ticketModelsUpdated;

You are changing the ticketModels pointer, but your adapter is always pointing to the previous ticketModel value.

You should also update your adapter list pointer.

0
Oleg Golomoz On

In the first case you share the same memory address of array elements between two links: ticketModels and ticketModelsUpdated. It means you can have access to the same elements from two different points of entry.

In the second case you create the same elements using new memory address. It means ticketModels isn't sharing the same memory space with ticketModelsUpdated.

Despite ticketModelsUpdated is local variable, allocated memory will live until there is reference to this address. So, in my understanding ticketModels should have access to all elements even after ticketModelsUpdated is destroyed. Both cases should work.

EDIT: Looks like you bind your adapter to the reference of ticketModels and not to the values of ticketModels. That is why your method ticketModels.clear() clears old allocated memory space where your adapter was linked to. So in the first case you change the link to new memory space for your ticketModels while your adapter still references to the cleared memory space. And in second case you keep the reference to the old memory space and fill cleared data there with new elements.

It's only my guess since you didn't provide any information how you bind your adapter with ticketModels.

0
Viswas Kg On

DIFFERENCE:

  • First: ticketModels = ticketModelsUpdated;
  • Second: ticketModels.addAll(ticketModelsUpdated);

First case: This case assigns the reference of ticketModelsUpdated to ticketModels. So whenever the "ticketModels" list gets modified, the "ticketModelsUpdated" also gets modified. That's why this case didn't work.

Second case: This case only assign the value of ticketModelsUpdated to ticketModels. So the changes in "ticketModels" list will not modify the "ticketModelsUpdated" list.