-1

I am trying to develop a mobile based flex application.

In my application, I have two views.

I am trying to pass the ArrayCollection as a data from one view to another view, but while trying to access ArrayCollection on the second view, I am getting an error ..

Here's code from firstView.mxml :

dirSteps is the arraycollection that I am trying to pass to next view ...

for (var r:Number = 0 ; r < directions.numRoutes; r++ ) {
                    var route:Route = directions.getRoute(r);

                    if (r >= 0 || r < (numRoutes - 1)) {
                        var midMarker:Marker = new Marker(route.endLatLng);
                        map.addOverlay(midMarker);
                    }
                    var numSteps:uint = route.numSteps;
                    for (var s:Number = 0 ; s < numSteps ; s++ ) {
                        var step:Step = route.getStep(s);
                        dirSteps.addItem({Step: (s+1), Description: step.descriptionHtml, Distance: step.distanceHtml, LatLng: step.latLng});
                    }
                }
                dirSteps.refresh(); 
 } 


 ..... some more code ...
 navigator.pushView(DetailDirection,dirSteps); 

Code from DetailDirection.mxml :

[Bindable]
private var directionList:ArrayCollection;

private function init():void {
    directionList = new ArrayCollection(ArrayUtil.toArray(data));

    // here, data should be my arraycollection, but throws above error on 
    // trying to access property (i.e Step, Distance etc .. ) of ArrayCollection ...
    trace(data.Distance);
}

Error: Unknown Property: 'Distance'. at mx.collections::ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()[E:\dev\4.y\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:870] at views::DetailDirection/init()[C:\Documents and Settings\ARSENAL\Adobe Flash Builder 4.6\CityExplorer_v2.0\src\views\DetailDirection.mxml:21] at views::DetailDirection/___DetailDirection_View1_creationComplete()[C:\Documents and Settings\ARSENAL\Adobe Flash Builder 4.6\CityExplorer_v2.0\src\views\DetailDirection.mxml:6] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:13152] at mx.core::UIComponent/set initialized()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:1818] at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:842] at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]


What is causing this error ? What needs to be done ?

Anything that I am missing here ?

Thanks

tomjerry
  • 105
  • 1
  • 3
  • 13

2 Answers2

0

Distance is not a documented property on an ArrayCollection that is why it throws an error.

the data property is usually a generic object; and the Flex Compiler generally does not throw compile errors when accessing properties directly against it.

You probably want to access an item in the ArrayCollection; something like this:

((data as ArrayCollection).getItemAt(0) as MyObjectType).distance
JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • What is `MyObjectType` here ? Which type of object you are talking about ? – tomjerry Mar 11 '12 at 04:44
  • ^ Thank you. Your soln worked. But, when I try to store value of `data` in my second view, like this .. `private var arr:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(data));` and then try to access as .. `((arr as ArrayCollection).getItemAt(0) as MyObjectType).distance` .. it still throws me the same error as "distance property unknown" ... – tomjerry Mar 11 '12 at 04:53
  • @tomjerry "MyObjectType" would be the object type of the objects in your dataProvider. When you initialize the ArrayCollection like that, it is set once; possibly before the data property is set. You'll have to step through code when you try to access the values to see what is wrong. Is the ArrayCollection valid? Does the item you're trying to access exist in the ArrayCollection? Is it cast properly? And finally, does it have a distance property? – JeffryHouser Mar 11 '12 at 13:03
0

here data is an ArrayCollection. So you can't directly access data.Distance

data[index] will give you the object , so data[index].Distance

e.g:

var data:ArrayCollection = new ArrayCollection();

data.addItem({name:"jack", distance:300});
data.addItem({name:"jill", distance:400});

trace(data[1].distance);  // prints 400

In your case use loop

private function init():void {

    for(var i:int = 0; i < data.length; i++){

        var item:Object = data[i];
        trace(item.Distance);

    }

}
Diode
  • 24,570
  • 8
  • 40
  • 51
  • Your soln works only if trying to access arraycollection in the same view. But I want to pass that arraycollection to secondview and access it. So, your soln might not work in that case. – tomjerry Mar 11 '12 at 04:42
  • I am just showing how to access the objects added to an ArrayCollection. You have to give index. – Diode Mar 11 '12 at 04:45
  • Yes, but how to access if I am trying to access it from 2nd view ? – tomjerry Mar 11 '12 at 04:47