Sum up all decimal value in programmatically created edit text

138 Views Asked by At

Please help, I want to create 30 edit text and get the sum of them. Currently, I am able to get the sum however this only work for whole number. when I enter decimal value the sum is not accurate. And the program crash when I delete the value in any one of the edit text, please advise on how I can prevent that. thank you!

 for (int i = 1; i < 31; i++) {
        EditText editText = new EditText(this);
        editText.setHint("Recording Length: " + "#" + i);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        editSampling.addView(editText);

        editText.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) {
                List<Double> getAverageLength= new ArrayList<Double>();
                String text = editText.getText().toString();
                double value = Double.parseDouble(text);
                getAverageLength.add(value);
                double sum = 0;
                for (int m = 0; m < getAverageLength.size(); m++)
                    sum = sum + getAverageLength.get(m) / getAverageLength.size();
                String toString = String.valueOf(sum);
                avgLength.setText(toString);
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
1

There are 1 best solutions below

0
On

The program logic is slightly faulty. Just use the following code:

List<Double> getAverageLength = new ArrayList<>();
final EditText avgLength; //or TextView... your choice
for (int i = 0; i < 30; i++) {
    getAverageLength.add(0);
    EditText editText = new EditText();
    editText.setHint("Recording Length: #"+(i+1));
    editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    editSampling.addView(editText);
    editText.addTextChangedListener(new TextWatcher() {
        ...
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String text = editText.getText().toString();
            double value = 0;
            if (text != null && !text.equals("")) {
                value = Double.parseDouble(text);
            }
            getAverageLength.set(i, value);
            double sum = 0;
            for (int m = 0; m < getAverageLength.size(); m++)
                sum += getAverageLength.get(m) / getAverageLength.size();
            String toString = String.valueOf(sum);
            avgLength.setText(toString);
        }
        ...
    });
}

EDIT: I forgot to point out the fault in your code. In your onTextChanged, you have re-initialised the list and appended only the current EditText's reading onto it. This may return the wrong sum. Moreover, you did not check whether the EditText's text string was null or blank. I don't see how this will fix your exact problem, though.