TextWatch does not update as it should

0

Good, I am trying to make a dynamic textview, one that receives data from a database, the other that shows the result of the operation of the first textview less what is being entered by the edittext, however I can not do that Show yourself how you should.

Here is the code:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pago_correo);
        saldoUsrAct = (TextView)findViewById(R.id.saldoact);
        saldototal = (TextView)findViewById(R.id.STotal);
        monto = (EditText)findViewById(R.id.transmonto);
        correodest = (EditText)findViewById(R.id.transcorreo);
        concepto = (EditText)findViewById(R.id.transconcepto);
        aceptar = (Button)findViewById(R.id.btn_enviar);
        cancelar = (Button)findViewById(R.id.btn_cancelar);




        saldito = 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) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                saldototal.setText(addNumbers());
            }
        };



        saldototal.addTextChangedListener(saldito);


    }

    private String addNumbers() {
        int number1;
        int number2;
        if(saldoUsrAct.getText().toString() != "" && saldoUsrAct.getText().length() > 0) {
            number1 = Integer.parseInt(saldoUsrAct.getText().toString());
        } else {
            number1 = 0;
        }
        if(monto.getText().toString() != "" && monto.getText().length() > 0) {
            number2 = Integer.parseInt(monto.getText().toString());
        } else {
            number2 = 0;
        }

        return Integer.toString(number1 - number2);
    }

}

Thanks in advance

    
asked by Jesus Moran 09.08.2017 в 03:42
source

2 answers

0

I see that you are placing this line of code:

saldototal.addTextChangedListener(saldito);

Being "saldototal" of type TextView ... try to put the TextWatcher to the corresponding EditText

    
answered by 09.08.2017 / 17:41
source
0

The problem may be that you are changing the saldototal text, and when you finish the operation and do saldototal.setText () it is changed again and the operation is performed again and so on. I think you should use another edit text to host the result of the operation. Anyway, you do not explain the problem, you just say that it does not work, but the problem is that it does not show the result? shows erroneous result? is the app stuck?

    
answered by 09.08.2017 в 04:14