clear + addAll vs re-initialise list RecyclerViewAdapter android
Asked Answered
S

0

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.

Seleucia answered 5/6, 2019 at 4:55 Comment(5)
May be they both refer to same array list, will need to see where both array lists originatesDandelion
The updatePostList method is inside the CustomRecyclerViewAdapter. The updatedPosts list is sent to this method from the Activity.Seleucia
The first point represents reusing the same list object. The Second point represents dereferencing and referencing the list from ActivityFitzsimmons
Yes but the question is why does calling this.postList.clear() clear updatedPosts too instead of just clearing postList.Seleucia
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?Dandelion

© 2022 - 2024 — McMap. All rights reserved.