EditText在滚动时在RecyclerView中丢失其值

问题描述 投票:0回答:1

起初我在我的Activity中使用了ListView但是我无法在我的edittext中解决我的问题,当滚动列表视图时值正在消失。因此,我将其更改为回收站视图,因为有人说它将在edittext中解决我的问题,但事实并非如此。当我滚动我的Recycler视图时,我的edittext的值会丢失,会发生什么。

这是我的适配器代码:

    public class RecyclerView_InvoiceList extends RecyclerView.Adapter<RecyclerView_InvoiceList.MyViewHolder> {

    private Context mContext;
    private List<InvoiceList> invoiceLists;
    private float TotalPaymentAmount;
    DBHelper dbHelper;
    int maxPaymentLineID;
    String maxPaymentID;
    String indicator;
    private final int requestCode = 20;

    String invoiceStatusValue, okay;

    float invAmountDue = 0;
    float invAmountPaid = 0;

    public RecyclerView_InvoiceList(Context mContext, List<InvoiceList> invoiceLists, Float totalPaymentAmount, String maxPaymentID
            , String okay, String Indicator, DBHelper dbHelper) {
        this.mContext = mContext;
        this.invoiceLists = invoiceLists;
        this.TotalPaymentAmount = totalPaymentAmount;
        this.maxPaymentID = maxPaymentID;
        this.okay = okay;
        this.indicator = Indicator;
        this.dbHelper = dbHelper;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // infalte the item Layout
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_invoice_custom, parent, false);
        // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
        return vh;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.SelectInvoiceCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                invoiceLists.get(getPosition).setSelectInvoice(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
            }
        });

        holder.PayFull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                invoiceLists.get(getPosition).setPayFull(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                if (holder.PayFull.isChecked()) {
                    holder.PayPartial.setChecked(false);
                }
            }
        });

        holder.PayPartial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                invoiceLists.get(getPosition).setPayPartial(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                if (holder.PayPartial.isChecked()) {
                    holder.PayFull.setChecked(false);
                }
            }
        });

        Calendar c = Calendar.getInstance();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        final String formattedDate = df1.format(c.getTime());

        try {
            dbHelper.openDataBase();

            // LAYOUT VISIBILITY FOR CREDIT MEMOs
            try {
                if (invoiceLists.get(position).getCredit_Memo_ID().equals(null) || invoiceLists.get(position).getCredit_Memo_ID().isEmpty()
                        || invoiceLists.get(position).getCredit_Memo_ID().length() < 1
                        || invoiceLists.get(position).getCredit_Memo_ID().trim().equals("")){
                    holder.CreditMemoID.setVisibility(View.GONE);
                    holder.CreditMemoDate.setVisibility(View.GONE);
                    holder.CreditMemoReason.setVisibility(View.GONE);
                    holder.LL2.setVisibility(View.GONE);
                    holder.LL3.setVisibility(View.GONE);
                } else {
                    holder.LL2.setVisibility(View.VISIBLE);
                    holder.LL3.setVisibility(View.VISIBLE);
                    holder.CreditMemoID.setVisibility(View.VISIBLE);
                    holder.CreditMemoDate.setVisibility(View.VISIBLE);
                    holder.CreditMemoReason.setVisibility(View.VISIBLE);

                    holder.CreditMemoID.setText(invoiceLists.get(position).getCredit_Memo_ID());
                    holder.CreditMemoDate.setText(invoiceLists.get(position).getCredit_Memo_Date().substring(0,10));
                    holder.CreditMemoReason.setText(invoiceLists.get(position).getCredit_Memo_Reason());
                }
            } catch (Exception e) {
                e.getMessage();
            }

            holder.SalesInvoiceNo.setText(invoiceLists.get(position).getSales_Invoice_ID());
            holder.InvoiceDate.setText(invoiceLists.get(position).getInvoice_Date());
            holder.DueDate.setText(invoiceLists.get(position).getDue_Date());

            float invAmount = 0;
            invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
            holder.InvoiceAmount.setText(String.format("%,.2f",invAmount));
            holder.AmountDue.setText(String.format("%,.2f",invAmount));
            //holder.InvoiceAmount.setText(invoiceLists.get(position).getInvoice_Amount());


            holder.TotalAmount.setTag(position); // This line is important.
            holder.SelectInvoiceCB.setTag(position); // This line is important.
            holder.PayFull.setTag(position); // This line is important.
            holder.PayPartial.setTag(position); // This line is important.


            holder.TotalAmount.setText(invoiceLists.get(position).getAmount_Paid());
            holder.SelectInvoiceCB.setChecked(invoiceLists.get(position).isSelectInvoice());
            holder.PayFull.setChecked(invoiceLists.get(position).isPayFull());
            holder.PayPartial.setChecked(invoiceLists.get(position).isPayPartial());

            try {
                if (invoiceLists.get(position).getAmount_Paid().toString().equals("") ||
                        invoiceLists.get(position).getAmount_Paid().toString().equals("0.0") ||
                        Float.parseFloat(invoiceLists.get(position).getAmount_Paid().toString()) < 1) {
                    invAmount = 0;
                    invAmountDue = 0;
                    invAmountPaid = 0;
                    //invAmount = Float.parseFloat(invoiceLists.get(position).getInvoice_Amount());
                    invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
                    invAmountDue = invAmount - invAmountPaid;
                    Log.e("Without AmountPaid ", "Amount Due : " + String.valueOf(invAmountDue));
                } else {
                    invAmount = 0;
                    invAmountDue = 0;
                    invAmountPaid = Math.round(Float.parseFloat(invoiceLists.get(position).getAmount_Paid())*100.00)/(float)100.00;
                    //invAmount = Float.parseFloat(invoiceLists.get(position).getInvoice_Amount());
                    invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
                    invAmountDue = invAmount - invAmountPaid;
                    Log.e("With AmountPaid ", "Amount Due : " + String.valueOf(invAmountDue));
                }

                holder.AmountDue.setText(String.format("%,.2f",invAmountDue));

            if (TotalPaymentAmount >= Float.parseFloat(String.valueOf(invAmountDue))) {
                holder.SelectInvoiceCB.setChecked(true);
                holder.PayFull.setChecked(true);
                holder.PayFull.setClickable(true);
                holder.PayPartial.setClickable(true);

                holder.TotalAmount.setText(String.valueOf(Math.round(invAmountDue * 100.00) / 100.00));

                System.out.println("TotalPaymentAmount: " + TotalPaymentAmount);
                System.out.println("invAmountDue: " + invAmountDue);

                if (Float.parseFloat(String.valueOf(invAmountDue)) < 1) {
                    Log.e("Computations : ", TotalPaymentAmount + " + " + String.valueOf(Float.parseFloat(String.valueOf(invAmountDue))));
                    Log.e("Equals ", "TotalPaymentAmount = " + TotalPaymentAmount);
                } else {
                    TotalPaymentAmount = TotalPaymentAmount - Float.parseFloat(String.valueOf(invAmountDue));
                }
            } else {
                if (TotalPaymentAmount < 1) {
                    holder.TotalAmount.setText("0.00");
                    /*holder.PayFull.setClickable(false);
                    holder.PayPartial.setClickable(false);*/

                } else {
                    holder.SelectInvoiceCB.setChecked(true);
                    holder.PayPartial.setChecked(true);
                    holder.PayFull.setClickable(true);
                    holder.PayPartial.setClickable(true);
                    holder.TotalAmount.setText(String.valueOf(Math.round(TotalPaymentAmount * 100.00) / 100.00));

                    if ((TotalPaymentAmount - Float.parseFloat(String.valueOf(invAmountDue))) < 0) {
                        TotalPaymentAmount = (float) 0.0;
                    } else {
                        TotalPaymentAmount = TotalPaymentAmount - Float.parseFloat(String.valueOf(invAmountDue));
                    }
                }
            }

        } catch (Exception e) {
            e.getMessage();
            System.out.println("Custom Invoice List: " + e);
        } finally {
            dbHelper.close();
        }
    }

    @Override
    public int getItemCount() {
        return invoiceLists.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox SelectInvoiceCB, PayFull, PayPartial;
        TextView SalesInvoiceNo, InvoiceDate, InvoiceAmount, AmountDue, DueDate, CreditMemoID, CreditMemoDate, CreditMemoReason;
        LinearLayout LL2, LL3;
        EditText TotalAmount;
        ImageView CaptureForm;

        public MyViewHolder(View view) {
            super(view);
            // get the reference of item view's
            SelectInvoiceCB = (CheckBox) view.findViewById(R.id.selectInvoiceCB);
            SalesInvoiceNo = (TextView) view.findViewById(R.id.SINo);
            InvoiceDate = (TextView) view.findViewById(R.id.SIDate);
            InvoiceAmount = (TextView) view.findViewById(R.id.SIAmount);
            AmountDue = (TextView) view.findViewById(R.id.SIAmountDue);
            DueDate = (TextView) view.findViewById(R.id.SIdueDate);
            PayFull = (CheckBox) view.findViewById(R.id.SIFull);
            PayPartial = (CheckBox) view.findViewById(R.id.SIPartial);
            TotalAmount = (EditText) view.findViewById(R.id.SITotalAmount);
            CreditMemoID = (TextView) view.findViewById(R.id.creditMemoID);
            CreditMemoDate = (TextView) view.findViewById(R.id.creditMemoDate);
            CreditMemoReason = (TextView) view.findViewById(R.id.creditMemoReason);
            LL2 = (LinearLayout) view.findViewById(R.id.ll2);
            LL3 = (LinearLayout) view.findViewById(R.id.ll3);
        }
    }
}
android android-recyclerview android-edittext recycler-adapter
1个回答
0
投票

问题:滚动后您的edittext值为0

解决方案:在Edittext方法textwatcher上使用此代码到afterTextchange()

码:

yourEditTextName.setSelection(yourEditTextName.getText().length());
© www.soinside.com 2019 - 2024. All rights reserved.