What is the difference between the location property of the constructor in Java – holder.getAdapterPosition() and onBindViewHolder().

What is the difference between the location property of the constructor in Java – holder.getAdapterPosition() and onBindViewHolder(). … here is a solution to the problem.

What is the difference between the location property of the constructor in Java – holder.getAdapterPosition() and onBindViewHolder().

Here is my code –

public void onBindViewHolder(myViewHolder holder, int position) {

1. details obj = list.get(holder.getAdapterPosition());
        2. details obj = list.get(position);

holder.position = position;
    }

I get a warning

Do not treat position as fixed; only use immediately and call
holder.getAdapterPosition() to look it up later RecyclerView will not
call onBindViewHolder again when the position of the item changes in
the data set unless the item itself is invalidated or the new position
cannot be determined. For this reason, you should only use the
position parameter while acquiring the related data item inside this
method, and should not keep a copy of it. If you need the position of
an item later on (e.g. in a click listener), use getAdapterPosition()
which will have the updated adapter position later.

So I’m confused about 1 and 2, which should I prefer? Why? As it says, getAdapterPosition() provides the updated location, and I’m getting the value from the list based on the location.

Thank you.

Related Problems and Solutions