Search code examples
androidandroid-layoutandroid-inflatetextwatcher

Calculation on textwatcher not working properly in android


I have made an android activity,In that multiple LinearLayouts are inflated on a "plus" button,Now in each Layout i am having 2 edittext and a textView,I want to do multiplication of that edittexts and display to textView,its working fine,But i am so much confused when multiple linearLayouts are binded,because i want all the textview values's sum at the end,I have tried as below,but getting wrong values.

code

add.setOnClickListener(new OnClickListener()){
@Override
onClick(){
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.raw_descs, null);
            ImageView buttonRemove = (ImageView) addView.findViewById(R.id.iv_del);
            et_item_id = (EditText) addView.findViewById(R.id.et_item_id);
            et_desc = (EditText) addView.findViewById(R.id.et_desc);
            et_qty = (EditText) addView.findViewById(R.id.et_qty);
            et_unit_prize = (EditText) addView.findViewById(R.id.et_unit_prize);
            et_amt = (EditText) addView.findViewById(R.id.et_amt);

            et_qty.addTextChangedListener(textwatcher);
            et_unit_prize.addTextChangedListener(textwatcher);

            buttonRemove.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    ((LinearLayout) addView.getParent()).removeView(addView);
                    // calculate();
                    if (cnt >= 0) {
                        cnt = cnt - 1;

                    }

                }
            });
            cnt = cnt + 1;
            listitems.setTag(cnt);

            listitems.addView(addView);
}

}
   private void calculateInvoice() {
    double QuantyInt = 1;
    double PriceInt = 0;
    double discountInt = 0;
    double shipInt = 0;
    for (int i = 0; i < listitems.getChildCount(); i++) {

        et_qty = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(4);
        et_amt = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(8);
        et_unit_prize = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(6);

        if (et_qty != null) {
            QuantyInt = Double.parseDouble(!et_qty.getText().toString().equals("") ? et_qty.getText().toString() : "0");

        }
        if (et_unit_prize != null) {
            PriceInt = Double.parseDouble(!et_unit_prize.getText().toString().equals("") ? et_unit_prize.getText().toString() : "0");

        }
        subtotal = (QuantyInt * PriceInt);

    }

    double textResult = subtotal;
    System.out.println("::::::::::::MY TOATAL PRICE::::::::::::::::>>>>>>>>>>>>>>>>" + subtotal);
    et_amt.setText("$" + textResult + "");

    double finaltotal = 0;

    for (int i = 0; i < subtotl.size(); i++) {
        System.out.println(":::::::::::::Subtotal values+++++++++++:::::::::::>>>>>>>>>>>>>>" + subtotl.get(i));
        finaltotal = finaltotal + Double.parseDouble(subtotl.get(i));

    }
    System.out.println(":::::::::::::Subtotal:::::::::::>>>>>>>>>>>>>>" + finaltotal);

    tv_pre.setText("$" + finaltotal + "");
    if (et_dis != null) {
        discountInt = Double.parseDouble(!et_dis.getText().toString().equals("") ? et_dis.getText().toString() : "0");

    }
    discount = ((finaltotal * discountInt) / 100);
    double txtdiscount = discount;
    tv_dis.setText("$" + txtdiscount + "");
    double totl1 = finaltotal - discount;
    tv_total.setText("$" + totl1 + "");

    if (et_ship != null) {
        shipInt = Double.parseDouble(!et_ship.getText().toString().equals("") ? et_ship.getText().toString() : "0");

    }
    tv_ship.setText("$" + et_ship.getText().toString());
    double fnltotl = (shipInt + totl1);
    tv_total.setText("$" + fnltotl + "");
}

TextWatcher textwatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        calculateInvoice();

    }
};

Solution

  • you don't need to get the String in editText which you registered a TextWatcher for that again . just pass that CharSequence from onTextChanged() to calculateInvoice(arg0) and do your calculation based on arg0.