我已经在我的应用程序中实现了Recycler View,并将数据渲染到其中。我有两个用于数量的加减按钮,但问题是每次滚动数量时都会改变。现在做什么。请帮忙。我还添加了getItemViewType,getItemId和getItemCount,但是我的列表在滚动时仍会更改其值。
代码:
public class AdapterReturnProduct extends RecyclerView.Adapter<AdapterReturnProduct.MyViewHolder> {
Context context;
ReturnProductFragment returnFragment;
ArrayList<ModelReturn> productDataArrayList;
DatabaseHelper databaseHelper;
ModelReturn estimationData;
int qty;
public AdapterReturnProduct(Context context, ArrayList<ModelReturn> productDataArrayList, ReturnProductFragment returnFragment) {
this.context = context;
this.productDataArrayList = productDataArrayList;
this.returnFragment = returnFragment;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_return, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
databaseHelper = new DatabaseHelper(context);
estimationData = productDataArrayList.get(position);
holder.txtProductName.setText(estimationData.getProduct_name());
holder.txtDelieveredValue.setText(estimationData.getProduct_ordered());
if (estimationData.getProduct_returned() == (null)) {
holder.etQty.setText("0");
} else {
holder.etQty.setText(String.valueOf(estimationData.getProduct_returned()));
}
if (estimationData.getProduct_type().equals("unit")) {
holder.imgPType.setImageResource(R.drawable.wine_icon_black);
holder.txtCodeValue.setText(estimationData.getProduct_barcode());
} else {
holder.imgPType.setImageResource(R.drawable.black_box);
holder.txtCodeValue.setText(estimationData.getProduct_barcode());
}
holder.ibPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ModelReturn estimationData = productDataArrayList.get(position);
qty = Integer.parseInt(holder.etQty.getText().toString());
qty = qty + 1;
holder.etQty.setText(qty + "");
returnFragment.upDateReturnedProduct(estimationData.getProduct_id(), estimationData.getProduct_name(),
estimationData.getProduct_barcode(), estimationData.getProduct_type(), estimationData.getProduct_ordered(), holder.etQty.getText().toString());
// deliveryFragment.updateProductInCart(estimationData.getProductId(), estimationData.getProductName(),
// estimationData.getProductBarcode(),
String.valueOf(qty),estimationData.getProductType());
}
});
holder.ibMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ModelReturn estimationData = productDataArrayList.get(position);
qty = Integer.parseInt(holder.etQty.getText().toString());
if (qty > 0) {
qty = qty - 1;
holder.etQty.setText(qty + "");
returnFragment.upDateReturnedProduct(estimationData.getProduct_id(), estimationData.getProduct_name(),
estimationData.getProduct_barcode(), estimationData.getProduct_type(), estimationData.getProduct_ordered(), holder.etQty.getText().toString());
}
}
});
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return productDataArrayList.size();
}
private static class ViewHolder {
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtProductName, txtCodeValue, txtDelieveredValue;
ImageButton ibMinus, ibPlus, ibDelete;
EditText etQty;
ImageView imgPType;
public MyViewHolder(View itemView) {
super(itemView);
txtProductName = (TextView) itemView.findViewById(R.id.txtProductName);
txtCodeValue = (TextView) itemView.findViewById(R.id.txtCodeValue);
txtDelieveredValue = (TextView) itemView.findViewById(R.id.txtDelieveredValue);
ibPlus = (ImageButton) itemView.findViewById(R.id.ibPlus);
ibMinus = (ImageButton) itemView.findViewById(R.id.ibMinus);
etQty = (EditText) itemView.findViewById(R.id.etQty);
imgPType = (ImageView) itemView.findViewById(R.id.imgPType);
}
}
}
答案 0 :(得分:0)
这里的问题是所显示的模型保持不变。仅在此处替换文字后:
holder.etQty.setText(qty + "");
该位置上的实际ModelReturn
仍然保留旧数量,因此,下次您再次滚动或触发onBindViewHolder
时,它将以旧值显示。您应该这样做:
ModelReturn estimationData = productDataArrayList.get(position);
// Also consider changing this to use estimationData.getQuantity() rather than reading from the textview
qty = Integer.parseInt(holder.etQty.getText().toString());
if (qty > 0) {
qty = qty - 1;
// These two lines RATHER than changing the text manually
// NOTE: Not sure how your setter is called, edit accordingly
estimationData.setQuantity(qty);
notifyDataSetChanged();
returnFragment.upDateReturnedProduct(estimationData.getProduct_id(), estimationData.getProduct_name(),
estimationData.getProduct_barcode(), estimationData.getProduct_type(), estimationData.getProduct_ordered(), holder.etQty.getText().toString());
}