当我单击项目上的增量符号时,它应该仅增加我单击的项目的数字。但是,我向下滚动,我发现其他一些项目也会增加。
这是我的代码
public class CartAdapter extends RecyclerView.Adapter<MyViewHolder> {
private Context mContext;
private List<Dish> dishList;
private CartFragment fra;
public CartAdapter(Context mContext, List<Dish> dishList, CartFragment fragment) {
this.mContext = mContext;
this.dishList = dishList;
this.fra=fragment;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_cart, parent, false);
final MyViewHolder holder = new MyViewHolder(itemView);
holder.inc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int i=Integer.parseInt(holder.number.getText().toString())+1;
holder.number.setText(i+"");
//fra.addPrice(p);
}
});
holder.dec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Integer.parseInt(holder.number.getText().toString())>1){
int i=Integer.parseInt(holder.number.getText().toString())-1;
holder.number.setText(i+"");
//fra.addPrice(-p);}
}
}});
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Dish dish = dishList.get(position);
holder.name.setText(dish.getName());
holder.ing.setText(dish.getIngredients());
holder.price.setText(dish.getPrice());
}
@Override
public int getItemCount() {
return dishList.size();
}
有什么问题?
我尝试在绑定视图上调用onClickListener,但同样的错误仍然存在。
答案 0 :(得分:0)
我找到了解决方案。
我需要在我的适配器中覆盖这两个方法
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}