如何根据适配器内的扣减金额(Edittext)更改总金额(Edittext)的值?这是屏幕
我所做的是当我勾选复选框时,扣除金额将自动获得其价值,甚至可以更改扣减金额的值。
我想要发生的是所有扣除金额与选中的复选框将汇总并显示在总金额编辑文本中。
这是我的代码: 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) {
}
});
这是我的适配器: 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);
}
}
答案 0 :(得分:0)
在列表视图适配器中获取已选中复选框的位置并将该值分配给编辑文本,如果用户取消选中该文本,则分配&#34;&#34;编辑文本,如果用户选中了复选框,则只允许用户编辑编辑文本,否则禁用编辑文本。
答案 1 :(得分:0)
我不确定我是否正确理解你的问题。但如果我理解正确,那就是你能做的 -
totalAmount
中创建一个类字段。boolean
数组,该数组将记录所有选中的复选框。EditText DeductionAmount
对象传递给适配器的构造函数。每当发生点击时,使用totalAmount
更新此editText对象中的金额。答案 2 :(得分:0)
您可以定义一个textwatcher来收听Deduction Amount EditText更改和nitify适配器。
在适配器中添加这些代码:
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;
}
在getView()mrthod:
中添加此textwatcher//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);
}
}
});
然后,您可以通过外部适配器添加此侦听器,并更改Total Amount(Edittext)值。
adapter.setOnEditTextValuesChangedListener(new MyAdapter.OnEditTextValuesChangedListener() {
@Override
public void onEditTextValuesChanged(CharSequence charSequence) {
//Change Total Amount here
}
});