0

How can I change the value of Total Amount (Edittext) based on Deduction Amount (Edittext) inside the adapter? Here's the screen

enter image description here

What I've done is when I checked on the checkbox the deduction amount will automatically have its value and they can even change the value of Deduction Amount.

What I want to happen is All deduction amount with the checked checkbox will sum up and display in the Total Amount edittext.

Here's my code: PaymentActivity.java:

AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(PaymentHeader.this);
    LayoutInflater inflater = getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.custom_deduction_select, null);

    alertdialogbuilder.setView(alertLayout);

    final Spinner TypesOfDeduction = (Spinner) alertLayout.findViewById(R.id.typeOfDeduction);
    final EditText DeductionRemarks = (EditText) alertLayout.findViewById(R.id.deductionRemarks);
    final EditText DeductionAmount = (EditText) alertLayout.findViewById(R.id.deductionAmount);
    final EditText PercentAmount = (EditText) alertLayout.findViewById(R.id.percentage);
    final CheckBox cbWithForm = (CheckBox) alertLayout.findViewById(R.id.cbWF);
    CaptureForm = (ImageView) alertLayout.findViewById(R.id.captureForm);
    final Button Cancel = (Button) alertLayout.findViewById(R.id.cancelBTN);
    final Button AddDeduction = (Button) alertLayout.findViewById(R.id.adddeductionBTN);
    final ListView selectInvoiceLV = (ListView) alertLayout.findViewById(R.id.selectInvoiceList);

    final AlertDialog alertDialog = alertdialogbuilder.create();
    alertDialog.setCanceledOnTouchOutside(false);
    alertDialog.show();

    dbHelper = new DBHelper(PaymentHeader.this);

    try {
        dbHelper.createDataBase();
        dbHelper.openDataBase();

        DeductionType = dbHelper.retrieveDeduction();
        DeductionTypeAdapter = new ArrayAdapter<String>
                (PaymentHeader.this, R.layout.spinner_single_line, DeductionType);
        DeductionTypeAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
        TypesOfDeduction.setAdapter(DeductionTypeAdapter);

        selectedSalesInvoices = dbHelper.retrieveTempInvoices(CustomerID);
        invoiceLists = dbHelper.retrieveInvoices(CustomerID);
        if (role.equals("Sales Representative")) {
            customListView_deductionInvoice = new CustomListView_DeductionInvoice
                    (this, selectedSalesInvoices, invoiceLists, TypesOfDeduction.getSelectedItem().toString());
        } else if (role.equals("District Manager")) {
            customListView_deductionInvoice = new CustomListView_DeductionInvoice
                    (this, selectedSalesInvoices, invoiceLists, TypesOfDeduction.getSelectedItem().toString());
        }
        selectInvoiceLV.setAdapter(customListView_deductionInvoice);
        DeductionAmount.setEnabled(false);

        TypesOfDeduction.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String ImageRequired = "";
                Percentage = "";

                dbHelper = new DBHelper(PaymentHeader.this);
                try {
                    dbHelper.createDataBase();
                    dbHelper.openDataBase();

                    String deductionID = dbHelper.getDeduction(TypesOfDeduction.getSelectedItem().toString(), "Deduction ID", "");
                    ImageRequired = dbHelper.getDeduction(TypesOfDeduction.getSelectedItem().toString(), "Image Required", "");
                    Percentage = dbHelper.getDeduction(deductionID, "Deduction Percentage", "");
                    //vatExempt = dbHelper.getCustomer(CustomerID, "", "VAT_Exempt");
                    Log.e("VAT EXEMPT ", vatExempt);

                    if (ImageRequired.equals("YES")) {
                        cbWithForm.setEnabled(true);
                    } else {
                        cbWithForm.setEnabled(false);
                    }


                } catch (Exception e) {
                    e.getMessage();
                }
                dbHelper.close();

                if (role.equals("Sales Representative")) {
                    customListView_deductionInvoice = new CustomListView_DeductionInvoice
                            (PaymentHeader.this, selectedSalesInvoices, invoiceLists, TypesOfDeduction.getSelectedItem().toString());
                } else if (role.equals("District Manager")) {
                    customListView_deductionInvoice = new CustomListView_DeductionInvoice
                            (PaymentHeader.this, selectedSalesInvoices, invoiceLists, TypesOfDeduction.getSelectedItem().toString());
                }
                selectInvoiceLV.setAdapter(customListView_deductionInvoice);
                customListView_deductionInvoice.notifyDataSetChanged();


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

Here's my adapter: CustomerListView_DeductionInvoice.java

public class CustomListView_DeductionInvoice extends BaseAdapter {
private Context mContext;
private List<SelectedSalesInvoices> selectedSalesInvoices;
DBHelper myDbHelper;
private List<InvoiceList> invoiceLists;

HashMap<String, String> SalesInvoiceNo = new HashMap<String, String>();
HashMap<String, String> Deduction_ID = new HashMap<String, String>();
HashMap<String, String> Deduction_Amount = new HashMap<String, String>();

String deductionType;
float invAmountDue = 0;
float invAmountPaid = 0;

float x = 0;

String deductionID, ttlDeduction;

public CustomListView_DeductionInvoice(Context mContext, List<SelectedSalesInvoices> selectedSalesInvoices, List<InvoiceList> invoiceLists,
                                       String deductionType) {
    this.mContext = mContext;
    this.selectedSalesInvoices = selectedSalesInvoices;
    this.invoiceLists = invoiceLists;
    this.deductionType = deductionType;
}

public HashMap<String, String> getSalesInvoiceNo() {
    return SalesInvoiceNo;
}

public HashMap<String, String> getDeduction_ID() {
    return Deduction_ID;
}

public HashMap<String, String> getDeduction_Amount() {
    return Deduction_Amount;
}

@Override
public int getCount() {
    return selectedSalesInvoices.size();
}

@Override
public Object getItem(int position) {
    return selectedSalesInvoices.get(position);
}

@Override
public long getItemId(int position) {
    return selectedSalesInvoices.get(position).getId();
}

public static class ViewHolder {
    CheckBox SelectInvoiceCB;
    TextView SalesInvoiceNo, InvoiceDate, InvoiceAmount, AmountDue, DueDate;
    EditText DeductionAmnt;
}

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

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

        view = View.inflate(mContext, R.layout.custom_deduction, null);
        holder = new CustomListView_DeductionInvoice.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.DeductionAmnt = (EditText) view.findViewById(R.id.deductionAmount);

        holder.SalesInvoiceNo.setText(selectedSalesInvoices.get(position).getSales_Invoice_ID());
        holder.InvoiceDate.setText(selectedSalesInvoices.get(position).getInvoice_Date());
        holder.InvoiceAmount.setText(selectedSalesInvoices.get(position).getInvoice_Amount());
        holder.DueDate.setText(selectedSalesInvoices.get(position).getDue_Date());

        myDbHelper = new DBHelper(mContext);

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

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

        } catch (Exception e) {
            e.getMessage();
            Log.e("Error - ", " 2nd Try :" + e);
        }

        final float invAmtFinal = invAmount;
        Log.e("", "DEDUCTION TYPE ADAPTER: " + deductionType);
        holder.SelectInvoiceCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.SelectInvoiceCB.isChecked()) {
                    if (deductionType.equals("BIR 2302")) {
                        computeDeduction(deductionType, invAmtFinal, holder, String.valueOf(position));
                    } else if (deductionType.equals("BIR 2306")) {
                        computeDeduction(deductionType, invAmtFinal, holder, String.valueOf(position));
                    } else if (deductionType.equals("BIR 2307")) {
                        computeDeduction(deductionType, invAmtFinal, holder, String.valueOf(position));
                    } else if (deductionType.equals("SENIOR CITIZEN")) {
                        computeDeduction(deductionType, invAmtFinal, holder, String.valueOf(position));
                    }
                } else {
                    holder.DeductionAmnt.setText("0.00");
                    Deduction_ID.remove(String.valueOf(position));
                    Deduction_Amount.remove(String.valueOf(position));
                }
            }
        });

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

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    if (holder.SelectInvoiceCB.isChecked()){
                        SalesInvoiceNo.put(String.valueOf(position), holder.SalesInvoiceNo.getText().toString());
                        Deduction_ID.put(String.valueOf(position), deductionID);
                        Deduction_Amount.put(String.valueOf(position), holder.DeductionAmnt.getText().toString());
                        Log.e("", "SalesInvoiceNo " + SalesInvoiceNo);
                        Log.e("", "Deduction_ID " + Deduction_ID);
                        Log.e("", "Deduction_Amount " + Deduction_Amount);
                        Log.e("ttlDeduction ", ttlDeduction);
                    }
                } catch (Exception e) {
                    e.getMessage();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

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

private void computeDeduction(String deductionType, float invAmtFinal, ViewHolder holder, String position) {
    deductionID = myDbHelper.getDeduction(deductionType, "Deduction ID", "");
    String Percentage = myDbHelper.getDeduction(deductionID, "Deduction Percentage", "");

    float ttlNetAmnt = 0;
    float ttlInvAmnt = invAmtFinal;
    float percent = 0;
    float decimal = 0;
    float totalDeduction = 0;

    ttlNetAmnt = ttlInvAmnt / (float) 1.12;

    percent = Float.parseFloat(Percentage);
    decimal = percent / 100;
    totalDeduction = decimal * ttlNetAmnt;

    holder.DeductionAmnt.setText(String.format("%,.2f", totalDeduction));

    SalesInvoiceNo.put(position, holder.SalesInvoiceNo.getText().toString());
    Deduction_ID.put(position, deductionID);
    Deduction_Amount.put(position, String.valueOf(totalDeduction));
    ttlDeduction = String.valueOf(totalDeduction);

    Log.e("", "SalesInvoiceNo " + SalesInvoiceNo);
    Log.e("", "Deduction_ID " + Deduction_ID);
    Log.e("", "Deduction_Amount " + Deduction_Amount);
    Log.e("ttlDeduction ", ttlDeduction);
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Eugene Santos
  • 61
  • 1
  • 11
  • This question can be closed as Too Broad (too much code) or Unclear or Lacking a [mcve], especially since under the answers below, the dialogue ended up with requests for free work and private email-based support. This is not what Stack Overflow is for. – halfer Sep 26 '18 at 12:32

3 Answers3

0

in your list view adapter get the position of the checked checkbox and assign the value to edit text simply, if user unchecks the same then assign "" to edit text and also if user has checked the checkbox then only allow user to edit the edit text else disable the edit text.

Nikhil Jadhav
  • 1,621
  • 2
  • 12
  • 19
0

I am not sure if I understand your problem correctly. But if I understand you correctly, this is what you can do -

  1. Make a class field in your adapter totalAmount.
  2. Maintain a boolean array that will keep account of all the checked checkboxes.
  3. Inside your onItemClickListener of Listview, check if the checkbox corresponding to that item has changed its state. You can use a boolean array to know this. If yes, then decrease or increase your total amount accordingly. Refer this link for this.
  4. You can pass the EditText DeductionAmount object to the constructor of the adapter. And whenever the click happens, update the amount in this editText object using totalAmount.
Sachin Aggarwal
  • 1,095
  • 8
  • 17
0

You can define a textwatcher to listen in Deduction Amount EditText changed and nitify adapter.

Add those code in your Adapter:

private TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(null != listener){
            listener.onEditTextValuesChanged(charSequence);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
};

private OnEditTextValuesChangedListener listener;
public interface OnEditTextValuesChangedListener{
    void onEditTextValuesChanged(CharSequence charSequence);
}
public void setOnEditTextValuesChangedListener(OnEditTextValuesChangedListener l){
    listener = l;
}

Add this textwatcher in getView() mrthod:

//Deduction Amount EditText
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if(b){
                editText.addTextChangedListener(textWatcher);
            }else{
                editText.removeTextChangedListener(textWatcher);
            }
        }
    });

Then you can add this listener by your adapter outside and change Total Amount (Edittext) value.

adapter.setOnEditTextValuesChangedListener(new MyAdapter.OnEditTextValuesChangedListener() {
        @Override
        public void onEditTextValuesChanged(CharSequence charSequence) {
            //Change Total Amount here
        }
    });
Deng
  • 58
  • 5
  • Hi Deng, I've done this and inside the adapter.setOnEditTextValuesChangedListener, i put a Log. But the log is not displaying. – Eugene Santos Aug 24 '17 at 04:26
  • and Deng what will I put inside TotalAmount.setText(); ? – Eugene Santos Aug 24 '17 at 04:28
  • this listener works when you edit your Deduction Amount (Edittext) ,and you can put setText() in onEditTextValuesChanged(),the param charSequence is your input into Deduction Amount (Edittext). – Deng Aug 24 '17 at 09:28
  • I've done everything you said sir. And inside the onEditTextValuesChanged(), I put TotalAmount.setText(charSequence). But nothing happens in totalAmount when i change the deductiln amount. – Eugene Santos Aug 24 '17 at 12:53
  • I tried to put a log in textWatcher onTextChanged inside the condition. Log.e("","charSequence:"+charSequence); and nothing shows in android monitor but when i put it outside the condition. The log always show the value of deductionAmount when i change it. – Eugene Santos Aug 24 '17 at 12:56
  • Sorry @deng i'm new to this. – Eugene Santos Aug 24 '17 at 12:56
  • the log that print successfully is when i put it outside the condition if(null != listener) in TextWatcher. – Eugene Santos Aug 25 '17 at 07:38
  • Log.e("", "Character Sequence!! Error: " + e); What about this log? – Deng Aug 25 '17 at 07:44
  • It Displays a nullPointerException for listener. – Eugene Santos Aug 25 '17 at 08:10