getAdapterPosition有时返回错误的值

huangapple go评论64阅读模式
英文:

getAdapterPosition somtimes returns wrong value

问题

我正试图为RecyclerView创建一个适配器,每个视图都包含一些信息和两个按钮,一个按钮用于删除视图,另一个按钮用于更新信息。我设置监听器如下,我得到了我想要的操作(即removeRecepiechooseNewRandomRecepie执行我想要的操作)。

然而,有时候,看起来是随机的,getAdapterPosition会发送错误的位置。因此,删除或更新会发生在错误的视图上(我已经测试了getAdapterPosition的输出,输出与更改的视图一致,即错误的视图)。我已经按照以下方式编写了setListener

void setListener(View view){
    Button removeButton = view.findViewById(R.id.remove_button);
    removeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            removeRecepie(getAdapterPosition());
        }
    });
    Button newRandomButton = view.findViewById(R.id.new_random);
    newRandomButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chooseNewRandomRecepie(getAdapterPosition());
        }
    });
}

如果有任何帮助或指导意见,我将非常感激。如果有任何进一步的信息可以帮助您帮助我,请告诉我:)

英文:

I am trying to create an adapter for a RecyclerView, where each view contains some information and two buttons, one for removing the view and one for updating the information. I set the listener as follows, and I get the actions to work as I want to (i.e. the removeRecepie and chooseNewRandomRecepie does what I want them to do).

However, sometimes, seemingly randomly, the getAdapterPosition sends the incorrect position. Thus the removal or updating happens to the incorrect view (I have tested the output of getAdapterPosition, and the output is consistent with the view that changes, i.e. the wrong one). I have written the setListener as follows:

    void setListener(View view){
        Button removeButton = view.findViewById(R.id.remove_button);
        removeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeRecepie(getAdapterPosition());
            }
        });
        Button newRandomButton = view.findViewById(R.id.new_random);
        newRandomButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseNewRandomRecepie(getAdapterPosition());
            }
        });
    }

I would be really grateful for any help, or pointers to help. Please let me know if there is any further information I can include to help you help me getAdapterPosition有时返回错误的值

答案1

得分: 0

你应该使用 onBindViewHolder

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.removeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            removeRecipe(position);
        }
    });
    holder.newRandomButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chooseNewRandomRecipe(position);
        }
    });
}

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    Button removeButton;
    Button newRandomButton;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        removeButton = itemView.findViewById(R.id.removebutton);
        newRandomButton = itemView.findViewById(R.id.newRandomButton);
    }
}

注意:我已经更正了拼写错误,将 "recepie" 更改为 "recipe"。

英文:

You should be use onBindViewHolder.

public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.removeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeRecepie(position);
            }
        });
        holder.removeButton.SetOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseNewRandomRecepie(position);
            }
        });
    }

    public class MyViewHolder extends  RecyclerView.ViewHolder implements View.OnClickListener {

        Button removeButton;
        Button newRandomButton;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            removeButton = itemView.findViewById(R.id.removebutton);
            newRandomButton = itemView.findViewById(R.id.newRandomButton);
        }
    }

huangapple
  • 本文由 发表于 2020年9月15日 18:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63900171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定