【发布时间】:2011-08-30 09:13:55
【问题描述】:
我目前正在尝试实现一个 Singleton 类,以存储项目的 ArrayCollection,然后我可以在我的应用程序的整个生命周期中访问和操作这些项目。我创建了下面的 Singleton 类,旨在保存 ArrayCollection 信息:
package valueObjects
{
import mx.collections.ArrayCollection;
[Bindable]
public class Model
{
private static var instance:Model = new Model();
public var ids:ArrayCollection = new ArrayCollection();
public function Model()
{
if(instance)
{
trace("New instance cannot be created. Use Singleton.getInstance()");
}
}
public static function getInstance():Model
{
return instance;
}
}
}
然后,我在我的应用程序的 Main Deafult 页面上创建了以下代码,以便在启动应用程序后立即填充 ArrayCollection:
import valueObjects.Model;
protected var models:Model = new Model();
private function loop():void
{
var index:int;
for( index = 0; index < compsCollection.length; index++ )
{
trace( "Element " + index + " is " + compsCollection[index].comp_id );
models.ids.addItem(compsCollection[index].comp_id);
trace(models.ids.length);
}
}
Singleton 类中的 ArrayCollection 正在被填充,因为我进入循环的跟踪语句清楚地显示了 ArrayCollection 中数据的构建。但是,当我移动到应用程序中的另一个视图时,我尝试使用以下代码在 Singleton 类中访问此 ArrayCollection:
import valueObjects.Model;
protected var models:Model = Model.getInstance();
protected var test:ArrayCollection = new ArrayCollection();
protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
{
var index:int;
trace("Array Length =" + models.ids.length);
for( index = 0; index < models.ids.length; index++ )
{
trace( "Element " + index + " is " + models.ids[index].comp_id );
test.addItem(models.ids[index].comp_id);
}
testbox.text = test.toString();
}
现在我遇到的问题是,当我尝试访问这个 ArrayCollection(ids) 时,由于某种原因它似乎是空的。我已经包含了一个跟踪语句,它还说 ArrayCollection 的长度是“0”。有人可以帮忙吗??
【问题讨论】:
标签: android apache-flex singleton flash-builder