How can I change the value of Total Amount (Edittext) based on Deduction Amount (Edittext) inside the adapter? Here's the screen
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);
}
}