mardi 7 novembre 2017

Checkbox inside List View automatically checking and un-checking when scroll

I know that this question has been asked over and over again but still I haven't found/understand anything that I found. The checkbox is unchecked whenever the list is scrolled down or some of the checkbox is checked whenever the list is scrolled up. Here is my Code. Please suggest a solution for this. Thank you.

@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
    //View v = View.inflate(mContext, R.layout.sales_invoice_custom,null);
    final ViewHolder holder;

    if (view == null) {
        view = View.inflate(mContext, R.layout.sales_invoice_custom, null);
        holder = new ViewHolder();

        holder.SelectInvoiceCB = (CheckBox) view.findViewById(R.id.selectInvoiceCB);
        holder.SalesInvoiceNo = (TextView) view.findViewById(R.id.SINo);
        holder.InvoiceDate = (TextView) view.findViewById(R.id.SIDate);
        holder.InvoiceAmount = (TextView) view.findViewById(R.id.SIAmount);
        holder.AmountDue = (TextView) view.findViewById(R.id.SIAmountDue);
        holder.DueDate = (TextView) view.findViewById(R.id.SIdueDate);
        holder.PayFull = (CheckBox) view.findViewById(R.id.SIFull);
        holder.PayPartial = (CheckBox) view.findViewById(R.id.SIPartial);
        holder.TotalAmount = (EditText) view.findViewById(R.id.SITotalAmount);
        holder.CreditMemoID = (TextView) view.findViewById(R.id.creditMemoID);
        holder.CreditMemoDate = (TextView) view.findViewById(R.id.creditMemoDate);
        holder.CreditMemoReason = (TextView) view.findViewById(R.id.creditMemoReason);
        holder.LL2 = (LinearLayout) view.findViewById(R.id.ll2);
        holder.LL3 = (LinearLayout) view.findViewById(R.id.ll3);

        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

    InvoicePopulate(holder,position);

    return view;
}


public void InvoicePopulate(final ViewHolder holder, final int position) {
    dbHelper = new DBHelper(mContext);

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


            //holder.SelectInvoiceCB.setChecked(false);
            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));


            try {
                if (invoiceLists.get(position).getAmount_Paid().toString().equals("") ||
                        invoiceLists.get(position).getAmount_Paid().toString().equals("0")) {
                    invAmount = 0;
                    invAmountDue = 0;
                    invAmountPaid = 0;

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

                final float finalInvAmount = invAmountDue;
                holder.PayFull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (holder.PayFull.isChecked()) {
                            holder.PayPartial.setChecked(false);
                            if (holder.SelectInvoiceCB.isChecked()) {
                                invoiceStatusValue = "PAID_FULL";
                                holder.TotalAmount.setText(String.valueOf(Math.round(finalInvAmount*100.00)/100.00));
                                //holder.TotalAmount.setText(holder.InvoiceAmount.getText().toString());
                            }
                        }
                    }
                });

                holder.PayPartial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (holder.PayPartial.isChecked()) {
                            holder.PayFull.setChecked(false);
                            if (holder.SelectInvoiceCB.isChecked()) {
                                invoiceStatusValue = "PAID_PARTIAL";
                                holder.TotalAmount.setText("0.00");
                            }
                        }
                    }
                });

                holder.AmountDue.setText(String.format("%,.2f",invAmountDue));
            } catch (Exception e) {
                e.getMessage();
            }

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

        }


    } catch (Exception e){
        e.getMessage();
        System.out.println("Error - " + e);
    } finally {
        dbHelper.close();
    }
}

Any suggestions will be a big help. Thank you!




Aucun commentaire:

Enregistrer un commentaire