This is correct when using onClick in onBindViewHolder to avoid "do not treat position as fixed only use immediately and call holder"?

Viewed 33

I am working on an application that on the main screen uses RecyclerView to show attendance information of different groups of people, each element contains a list of the last 3 saved records ("Latest records" in the image):

enter image description here

Clicking the "Watch" button should display the attendance record for the corresponding date...

It turns out that sometimes there are only one or 2 attendance records, so I have to check the size of the list to assign the onClickListener of each element.

So if there are 3 or more items to display, you would have to add the code for the click events for date 1, date 2 and date 3. In the event that there are 2 elements, it would only be for date 2 and date 3. Lastly if there is only 1 element, I would add click event for date 3 only.

As you can imagine, there would be a lot of repeated code, so I decided to add the code for each date in different functions.

So here my question starts because commonly when I put the code for a click event in onBindViewHolder and use the position variable, it regularly shows me the warning "do not treat position as fixed only use immediately and call holder". I understand the problem but I realized that when I pass the position variable as a parameter to my click functions it doesn't show me any warning, I guess it's because Android Studio doesn't know that there is a setOnClickListener inside the functions.

So, these are the parameters that my function was using: private void clcikDate1(TextView tvRecord1, int position, long idGroup, String date){ in this approach position was obtained from onBindViewHolder using holder.getAdapterPosition() I know this is wrong and now I show you the current approach:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Group group = (Grupo) mGroups.get(position);
    .
    .
    .
    .
    .
    long idGroup = group.getId;
    List<AttendanceRecord> recordList = interfaceDB.getLast3Records(idGroup);
    if (recordList.size() == 3){
        //Visual elements code....
        clcikDate1(holder, date1);
        clcikDate2(holder, date2);
        clcikDate3(holder, date3);
    } else if (recordList.size() == 2){
        //Visual elements code....
        clcikDate2(holder, date2);
        clcikDate3(holder, date3);
    } else {
        //Visual elements code....
        clcikDate3(holder, date3);
    }
}

private void clcikDate1(ViewHolder holder, String date){
    int lastPosition = holder.getAdapterPosition();
    long idGroup = mGroups.get(lastPosition).getId();
    holder.tvRecord1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clicssListener.seeRecord1Click(lastPosition, idGroup, date);
        }
    });
}

As you can see now I pass the ViewHolder as a parameter and inside the function I get the TextView and the last position, my question is, is this correct? and another question would be: Is it necessary to get the idGroup variable again in the click function? because as you can see I have one in onBindViewHolder but I don't know if it is necessary to update in my click function

0 Answers
Related