0

I am making a program where people can create their own food schedule. I also want to calculate the total amount of calories per food schedule.

So first off people can add items to a list which are saved in an arraycollection:

    <s:List includeIn="State2" x="12" y="533" width="428" height="157" dataProvider="{acKoop}"
            enabled="true" change="totalcal(event)">
        <s:itemRenderer>
            <fx:Component>
                <mx:Label text="{data.Type} {data.Kcal}" />
            </fx:Component>
        </s:itemRenderer>
    </s:List>

I want a function that retrieves all the values of data.Kcal and then makes a Sum of it.

public function totalcal(event:Event):void{
            var price:Number=acKoop[event.columnIndex].Kcal;
            total += price;

        }
ketan
  • 19,129
  • 42
  • 60
  • 98
Munlau Yau
  • 21
  • 6
  • Please check this **[LINK](https://dl.dropboxusercontent.com/u/108036807/ExListRenderer_01.swf)**, and click over the list component, and tell me if that will be useful. Anyway i do not undestand why you need recalculate the total in change event, when you **renderer** always the same because is a **LABEL**, you can do this before, because the information is in the dataprovider. – Gaston Flores Aug 14 '13 at 14:47
  • Well this is perfect. Could you give me the code for that? – Munlau Yau Aug 15 '13 at 04:13

1 Answers1

0

Here the code of link I was send, maybe will be useful:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            [Bindable]
            private var acKoop:ArrayCollection = null;

            public function init():void{
                var arr:ArrayCollection = new ArrayCollection();
                var obj:Object = new Object();
                obj.Type = "TYPE1";
                obj.Kcal = 10;
                arr.addItem(obj);
                obj = new Object();
                obj.Type = "TYPE2";
                obj.Kcal = 50;
                arr.addItem(obj);
                acKoop = arr;
            }

            public function totalcal(event:Event):void{
                var i:Number = 0;
                for each(var obj:Object in ArrayCollection(List(event.currentTarget).dataProvider)){
                    i = i + obj.Kcal;
                }
                Alert.show("Total of Kcal = " + i.toString());
            }
        ]]>
    </fx:Script>

    <s:List dataProvider="{acKoop}"
            enabled="true" change="totalcal(event)">
        <s:itemRenderer>
            <fx:Component>
                <mx:Label text="{data.Type} {data.Kcal}" />
            </fx:Component>
        </s:itemRenderer>
    </s:List>
</s:Application>

Basically, on the change event I take the dataprovider information from the event, and calculate the TOTAL with the for each loop. I hope this will be useful.

Gaston Flores
  • 2,457
  • 3
  • 23
  • 42
  • Now I have to click on the List first before it starts calculating. How can I start the for each loop without having to click on the List? – Munlau Yau Aug 15 '13 at 18:28
  • @MunlauYau, if you need calculate before, then put at the end of **init()** method the code of method **totalcal()**, and change `ArrayCollection(List(event.currentTarget).dataProvider)` to `acKoop`. Try that and then tell me if this works. At what time you load a dataprovider? – Gaston Flores Aug 16 '13 at 14:54