Android 回收器视图更改滚动条上的项目值

Android 回收器视图更改滚动条上的项目值,android,android-fragments,scrollview,android-recyclerview,Android,Android Fragments,Scrollview,Android Recyclerview,我在我的应用程序中实现了一个Recycler视图,并将数据呈现到其中。我有两个加减按钮的数量,但问题是每次当我滚动数量将改变。现在该怎么办。请帮忙。我还添加了getItemViewType、getItemId和getItemCount,但我的列表仍然会在滚动时更改其值 代码: 公共类AdapterReturnProduct扩展了RecyclerView.Adapter{ 语境; ReturnProductFragment returnFragment; ArrayList productData

我在我的应用程序中实现了一个Recycler视图,并将数据呈现到其中。我有两个加减按钮的数量,但问题是每次当我滚动数量将改变。现在该怎么办。请帮忙。我还添加了getItemViewType、getItemId和getItemCount,但我的列表仍然会在滚动时更改其值

代码:

公共类AdapterReturnProduct扩展了RecyclerView.Adapter{
语境;
ReturnProductFragment returnFragment;
ArrayList productDataArrayList;
数据库助手数据库助手;
模型回归估计数据;
整数数量;
公共适配器ReturnProduct(上下文上下文、ArrayList productDataArrayList、ReturnProductFragment returnFragment){
this.context=上下文;
this.productDataArrayList=productDataArrayList;
this.returnFragment=returnFragment;
}
@非空
@凌驾
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
最终视图itemView=LayoutInflater.from(parent.getContext())
.充气(R.layout.item_返回,父项,假);
返回新的MyViewHolder(itemView);
}
@凌驾
public void onBindViewHolder(@NonNull final MyViewHolder holder,final int position){
databaseHelper=新的databaseHelper(上下文);
estimationData=productDataArrayList.get(位置);
holder.txtProductName.setText(estimationData.getProduct_name());
holder.txtDelieveredValue.setText(estimationData.getProduct_ordered());
if(estimationData.getProduct_returned()==(null)){
持有人etQty.SETEXT(“0”);
}否则{
holder.etQty.setText(String.valueOf(estimationData.getProduct_returned());
}
if(estimationData.getProduct_type()等于(“单位”){
holder.imgPType.setImageResource(R.drawable.wine_图标_黑色);
holder.txtCodeValue.setText(estimationData.getProduct_barcode());
}否则{
holder.imgPType.setImageResource(R.drawable.black_box);
holder.txtCodeValue.setText(estimationData.getProduct_barcode());
}
holder.ibPlus.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ModelReturn estimationData=productDataArrayList.get(位置);
qty=Integer.parseInt(holder.etQty.getText().toString());
数量=数量+1;
持有者数量设置文本(数量+“”);
returnFragment.upDateReturnedProduct(estimationData.getProduct_id(),estimationData.getProduct_name(),
estimationData.getProduct_barcode()、estimationData.getProduct_type()、estimationData.getProduct_ordered()、holder.etQty.getText().toString());
//deliveryFragment.UpdateProductInvot(estimationData.getProductId(),estimationData.getProductName(),
//estimationData.getProductBarcode(),
String.valueOf(qty),estimationData.getProductType();
}
});
holder.ibemission.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ModelReturn estimationData=productDataArrayList.get(位置);
qty=Integer.parseInt(holder.etQty.getText().toString());
如果(数量>0){
数量=数量-1;
持有者数量设置文本(数量+“”);
returnFragment.upDateReturnedProduct(estimationData.getProduct_id(),estimationData.getProduct_name(),
estimationData.getProduct_barcode()、estimationData.getProduct_type()、estimationData.getProduct_ordered()、holder.etQty.getText().toString());
}
}
});
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
public int getItemViewType(int位置){
返回位置;
}
@凌驾
public int getItemCount(){
返回productDataArrayList.size();
}
私有静态类视图持有者{
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
TextView txtProductName、txtCodeValue、txtDelieveredValue;
ImageButton IB负号、ibPlus、ibDelete;
编辑文本etQty;
ImageView-imgPType;
公共MyViewHolder(查看项目视图){
超级(项目视图);
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);
}
}
}

这里的问题是显示的模型保持不变。仅在此处替换文本之后:

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());
}

设置适配器使用适配器后,您需要通知适配器数据集已更改。notifydatasetchanged()@mishti您解决了它还是尝试了我的解决方案?@Vucko我解决了它。ThanksIt没有向我显示旧值导致更改为任何数字我不知道您刚才说了什么。请用标点符号。试着更具描述性。是的,它向您显示了旧的值,因为它依赖于模型,而不是您看到的文本视图中的内容。模型需要始终与显示的内容保持一致。它不是从数据库中获取旧值。当我阴囊
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());
}