从RecyclerView适配器获取EditText值

时间:2018-12-28 09:37:49

标签: android android-recyclerview android-edittext

我正在尝试从RecyclerView(Adapter)获取EditText值。

 class BillingOrderListHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView Product, type,vartie,unitnumber, Total;

    LinearLayout CardView;

    BillingOrderListHolder (View itemView) {
        super(itemView);

        Product = itemView.findViewById(R.id.Product);
        type = itemView.findViewById(R.id.type);
        vartie = itemView.findViewById(R.id.vartie);
        unitnumber=   itemView.findViewById(R.id.unitnumber);
        rate =   itemView.findViewById(R.id.rate);
        Total=   itemView.findViewById(R.id.Total);

        rate.addTextChangedListener(new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                if (!charSequence.toString().equalsIgnoreCase("")) {

                    int result = (dataList.get(getAdapterPosition()).getUnitCount() * Integer.valueOf(charSequence.toString()));
                    Total.setText(" = Rs:" + String.valueOf(result));
                    onEditTextChanged.onTextChanged(i, charSequence.toString());
                }
                dataList.get(getAdapterPosition()).setEditTextValue(rate.getText().toString());


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

如果用户更改EditText中的值,则会相应地计算总数。我只想从RecyclerView中获取EditText值,然后生成总金额的发票。

int sNO = 0, qTY = 0, TotalCharges = 0, Price = 0;
    String ProductType, Type, Veriety;

    for (int i = 0; i < DeliverOrderlist.size(); i++){

        sNO = i + 1;

        ProductType = DeliverOrderlist.get(i).getProduct();
        Type = DeliverOrderlist.get(i).getType();
        serilno.addElement(new Paragraph(String.valueOf(sNO)));
        Veriety = DeliverOrderlist.get(i).getVariety();
        qTY = DeliverOrderlist.get(i).getUnitCount();
        qty.addElement(new Paragraph(String.valueOf(qTY)));
        description.addElement(new Paragraph(ProductType + " " + Type + " " + Veriety));
      Price = Integer.parseInt(Billing_InvoiceAdapter.dataList.get(i).getEditTextValue());

        price.addElement(new Paragraph(String.valueOf(Price)));
        int result = qTY * Price;
        amount.addElement(new Paragraph(String.valueOf(result)));

            TotalCharges += result;
    }

当我运行上面的代码时,如果不更改EditText中的值,它会很好地工作。如果我更改了recyclerView中任何一个EditText中的值,它将从recyclerView中获取最后一个EditText框值,并将该值设置为我编辑的EditText值。如果我对信息不明确,很抱歉。我做错什么了吗?可以

我也尝试了this,但是只有在EditText中编辑该值时它才起作用。否则会给出空点异常错误。有人可以帮我吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

您需要先在bindviewholder中将监听器设置为空,然后在bindviewholder方法中添加如下所示的监听器。

billingListHolder.rate.addTextChangedListener(null);

billingListHolder.rate.addTextChangedListener(new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                if (!charSequence.toString().equalsIgnoreCase("")) {

                    int result = (dataList.get(getAdapterPosition()).getUnitCount() * Integer.valueOf(charSequence.toString()));
                    Total.setText(" = Rs:" + String.valueOf(result));
                    onEditTextChanged.onTextChanged(i, charSequence.toString());
                }
                dataList.get(getAdapterPosition()).setEditTextValue(rate.getText().toString());


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });