【问题标题】:Recycler View changes item value on scroll回收站视图在滚动时更改项目值
【发布时间】:2019-07-18 09:25:46
【问题描述】:

我已经在我的应用程序中实现了一个 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);
        }
    }
}

【问题讨论】:

  • 设置适配器使用adapter.notifydatasetchanged()后需要通知适配器数据集发生变化
  • @mishti 你解决了还是试试我的解决方案?
  • @Vucko 我解决了。谢谢

标签: android android-fragments scrollview android-recyclerview


【解决方案1】:

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

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

【讨论】:

  • 它没有显示旧值导致更改为任何数字
  • 我不知道你刚才说了什么。请使用标点符号。并尝试更具描述性。是的,它向您展示了旧值,因为它依赖于模型,而不是您看到的文本视图中的内容。模型需要始终符合显示的内容。
  • 它没有从数据库中获取旧值。当我滚动它时,列表项只需在 textview 上选择任何随机数
  • 你有没有试过把我的代码放进去看看有没有帮助?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多