0

At first I used ListView in my Activity but I can't solve my problem in my edittext which is the value is disappearing when the list view is scrolled. So, I changed it to recycler view because someone said it will solved my issue in edittext but its not. What happens is when I scroll my recycler view the value of my edittext in it loses.

Here's my code in Adapter:

    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);
        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Eugene Santos
  • 61
  • 1
  • 11
  • 1
    Possible duplicate of [EditText loses focus on Scroll in RecyclerView](https://stackoverflow.com/questions/45238973/edittext-loses-focus-on-scroll-in-recyclerview) – Goku Nov 21 '17 at 09:04
  • The whole point of a `RecyclerView` is to reuse the views so that the phone doesn't have to keep all the rows in memory at once and doesn't have to be constantly destroying and creating views. When your `EditText` leaves the screen, the phone takes that view, resets its contents and moves it to the bottom of the incoming view stack. – Goku Nov 21 '17 at 09:08
  • @Prem Hi Sir, I don't know how will i do it. Can you please make help me on how can I code it? – Eugene Santos Nov 21 '17 at 09:15
  • it is not possible you have to save value of editext default value – Goku Nov 21 '17 at 09:17
  • @Prem Where will I save it? HashMap, ArrayList or List? – Eugene Santos Nov 21 '17 at 09:20
  • its your choice @Eugene but its very difficult – Goku Nov 21 '17 at 09:21
  • @Prem Do you have any suggestion sir? I really need it coz I'm putting 10's to 100's of items in my RecyclerView and in each item there is a Edittext. – Eugene Santos Nov 21 '17 at 09:23
  • isnt putting boolean value in InvoiceList like `boolean edited = false` and changing it to true after edited solving your problem ? – Murat Güç Nov 21 '17 at 10:07
  • See also [Saving EditText content in RecyclerView](https://stackoverflow.com/questions/31844373/saving-edittext-content-in-recyclerview). – CoolMind Jun 30 '21 at 17:09

2 Answers2

0

Problem: Your edittext value is 0 after Scrolling

Solution: Use this code to your Edittext on textwatcher method afterTextchange()

Code:

yourEditTextName.setSelection(yourEditTextName.getText().length());
jwpfox
  • 5,124
  • 11
  • 45
  • 42
0

I had face a similar problem where I have used editText inside a recyclerview, losing editText value either because of scroll or because of softkeypad (when minimized)

implemented this function inside onBindViewHolder() for the edit Text of interest

setOnFocusChangeListener(new View.OnFocusChangeListener() {
            
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            //got the value from editText and stored in the data list
        }

    }
}
Jan
  • 4,974
  • 3
  • 26
  • 43
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 28 '22 at 20:06