gpt4 book ai didi

android - 如何修复返回其他数字的 onClick cardView

转载 作者:行者123 更新时间:2023-11-30 05:01:22 24 4
gpt4 key购买 nike

我有一个回收站,它显示了元素 list 、价格和状态。当我点击卡片时,我希望它显示我点击的商品的价格。但是无论何时,我都会点击。该卡返回其他卡的数据,但没有我点击的特定卡。

    public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemHolder> {
private TextView viewName, viewPrice;
private CheckBox viewAvailability;

private Context context;
private List<Item> listItems;

@NonNull
@Override
public ItemAdapter.ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
return new ItemAdapter.ItemHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ItemAdapter.ItemHolder holder, int position) {
Item item = listItems.get(position);
holder.setDetails(item);

}

@Override
public int getItemCount() {
return listItems.size();
}


public class ItemHolder extends RecyclerView.ViewHolder {
public ItemHolder(final View itemView) {
super(itemView);
viewName = itemView.findViewById(R.id.viewName);
viewPrice = itemView.findViewById(R.id.viewPrice);
viewAvailability = itemView.findViewById(R.id.viewAvailability);


itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


}
});
}

public void setDetails(Item item) {
viewName.setText(item.getName());
viewPrice.setText(item.getPrice());
int as = item.getAvailabilityStatus();

if (as == 1) {
viewAvailability.setChecked(true);
} else {
}
}
}

public ItemAdapter(Context context, List<Item> objects) {
this.context = context;
this.listItems = objects;
}

最佳答案

试试这个代码,把 itemView onclick listner 放在 setDetails 里面

public void setDetails(Item item) {
viewName.setText(item.getName());
viewPrice.setText(item.getPrice());
int as = item.getAvailabilityStatus();

if (as == 1) {
viewAvailability.setChecked(true);
} else {
}

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

// here you will able to access valid item
}
});
}

关于android - 如何修复返回其他数字的 onClick cardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com