Cleaven
Cleaven

Reputation: 974

Update Edittext when other Edittext change

I'm trying to update a single EditText based off other EditTexts. For some reason everything works fine until I have to set the text then I get a continuous list of errors. I can't seem to find out why or if this is the best practice. Any help would be appreciated. If I comment the setting of the text out. My app works fine. This is my code:

 final Boolean[] hasValue = {false};
    ArrayList<EditText> editTexts = new ArrayList<>(); // Container list
    ArrayList<Double> values = new ArrayList<>(); // value list
    editTexts.add(IBalance); // editTexts[0]
    editTexts.add(Trading); // editTexts[1]
    editTexts.add(Binary); // editTexts[2]
    editTexts.add(Referrals); // editTexts[3]
    editTexts.add(Leader1); // editTexts[4]
    editTexts.add(Leader2); // editTexts[5]
    editTexts.add(Deposit); // editTexts[6]
    editTexts.add(Withdrawal); // editTexts[7]
    editTexts.add(Total); // editTexts[8]

    for (final EditText editText : editTexts) { //need to be final for custom behaviors
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                /*if (editText.getText().toString().equals("0")) {
                    editText.setText("");

                }*/
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (editText.getText().toString().equals("") || editText.getText().toString().equals("0")) {
                    editText.setText("0.0");
                    editText.setSelection(editText.getText().length());
                }
                else if(editText.getText().toString().equals("0.0"))
                    editText.setSelection(editText.getText().length());
            }

            @Override
            public void afterTextChanged(Editable s) {
                values.add(Double.parseDouble(IBalance.getText().toString())); //[0]
                values.add(Double.parseDouble(Trading.getText().toString())); //[1]
                values.add(Double.parseDouble(Binary.getText().toString())); //[2]
                values.add(Double.parseDouble(Referrals.getText().toString())); //[3]
                values.add(Double.parseDouble(Leader1.getText().toString())); //[4]
                values.add(Double.parseDouble(Leader2.getText().toString())); //[5]
                values.add(Double.parseDouble(Deposit.getText().toString())); //[6]
                values.add(Double.parseDouble(Withdrawal.getText().toString())); //[7]
                for (final Double db : values) {
                    hasValue[0] = db != null;
                }
                double total = 0;
                if(hasValue[0]=true)
                {
                    total = values.get(0)+values.get(1)+values.get(2)+values.get(3)+values.get(4)+values.get(5)+values.get(6)-values.get(0);
//ERROR OCCURS      Total.setText(String.valueOf(total)); //ERROR OCCURS
                }
                values.clear();
            }
        });
    }

And this is a screen of error in the run console:erorr

It repeats itself for so long that the beginning of the error is cut off. this is how the app looks:

app

Upvotes: 0

Views: 29

Answers (2)

Tobi
Tobi

Reputation: 888

Your problem is, that you also add total to your List. That's why total gets also the Listener.

Setting the text of an EditText within its own Listener creates an endless loop.

Remove this line

editTexts.add(Total); // editTexts[8]

Upvotes: 1

Harsh Modani
Harsh Modani

Reputation: 221

Your checking whether the values are null is incorrect.

  1. Replace boolean[] hasValue = {false} with boolean hasValue = true.
  2. Instead of this,
for (final Double db : values) {
    hasValue[0] = db != null;
}

Use this:

for (final Double db : values) {
    if (db == null) {
        hasValue = false;
        break;
    }
}

This is because in the original one, you edited hasValue to depend only on the last element of values.

Also, in the total = ... line, you used values.get(0)-values.get(0). This aroused my curiosity.

Upvotes: 1

Related Questions