1

What's the different between 1 and 2:

1.

public void updatePostList(ArrayList<Post> updatedPosts) {
   this.postList.clear();
   this.postList.addAll(updatedPosts);
   notifyDataSetChanged();
}

2.

public void updatePostList(ArrayList<Post> updatedPosts) {
   this.postList = updatedPosts;
   notifyDataSetChanged();
}

For me, #1 doesn't work as calling this.postList.clear() clears updatedPosts too instead of just clearing postList and I can't understand why.

Rohan Taneja
  • 9,687
  • 3
  • 36
  • 48
  • 1
    May be they both refer to same array list, will need to see where both array lists originates – Ramees Thattarath Jun 05 '19 at 05:04
  • The `updatePostList` method is inside the `CustomRecyclerViewAdapter`. The `updatedPosts` list is sent to this method from the `Activity`. – Rohan Taneja Jun 05 '19 at 05:09
  • The first point represents reusing the same `list` object. The Second point represents dereferencing and referencing the `list` from `Activity` – Android Jun 05 '19 at 05:15
  • Yes but the question is why does calling `this.postList.clear()` clear `updatedPosts` too instead of just clearing `postList`. – Rohan Taneja Jun 05 '19 at 05:45
  • I think post list is the array list passed to constructor of adapter from activity, may be you are calling updatePostList with the same array list from activity, csn u pls share your activity code? – Ramees Thattarath Jun 05 '19 at 06:18

0 Answers0