How to average out values of Textview inside a listView

548 Views Asked by At

I 'm devoloping an app with a listview , every row is populated with various objects including a Textview with int value, how can I average out the valus contained in the textview in my listview ? thanks

2

There are 2 best solutions below

1
On BEST ANSWER

you should implement a method of counting the average values in your adapter

public class Adapter extends BaseAdapter{

        ......

        public int getAverageValue(){
            int result = 0;
            if(getCount() > 0) {
                for (int i = 0; i < getCount(); i++) {
                    AdapterDataItem item = getItem(i);
                    result += item.getYourInt();
                }
                result = result/getCount();
            }
            return result;
        }
    }

this is not the end result you will need to adjust for themselves

0
On

Assuming the calculation of that average will be performed on some view click which is not a part of ListView.

In ListView's Adapter you must be passing the ArrayList of some Java Model class. That class will have one variable which will represent the value entered by user. You have to store the value on focus change listener.

After user click the Calculate Average button. You simply iterate through the ArrayList and apply the formula of average i.e. Sum of All Values/ Number of Values