0

我正在尝试将 ArrayList 中的数据绑定到列表,但这里只有最后一个元素显示在 list(99) 上,而不是 arraylist 的全部内容。

private function completeHandler(event:Event):void
        {
            var xmlData:XML = XML(event.target.data);
            trace(xmlData);
             var i:int = 0;
            for (i;i<100;i++)
            {
                var arr:ArrayList = new ArrayList();
                arr.addItem(i);
                trace(arr);
            } 
            list.dataProvider = arr;
        }

我不知道该怎么做?

4

2 回答 2

0

检查此代码,这将对您有所帮助,

您可以根据列表的行数浏览所有数据....

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  minWidth="955" minHeight="600">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            [Bindable]private var _index:int = 0;
            private var _coll:ArrayCollection = new ArrayCollection([{name:'ashish',age:'28'},{name:'abhi',age:'29'},{name:'kunal',age:'27'},
                {name:'ashish1',age:'28'},{name:'abhi1',age:'29'},{name:'kunal1',age:'27'},
                {name:'ashish2',age:'28'},{name:'abhi2',age:'29'},{name:'kunal2',age:'27'},
                {name:'ashish3',age:'28'},{name:'abhi3',age:'29'},{name:'kunal3',age:'27'}]);

            protected function button1_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                if((_index-li.rowCount>=0))
                    _index =  _index - li.rowCount;
            }


            protected function button2_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                if((_index+li.rowCount<_coll.length))
                    _index =  _index + li.rowCount;
            }

        ]]>
    </mx:Script>

    <mx:List id="li" dataProvider="{_coll.source.slice(_index,(_index+li.rowCount))}" labelField="name" rowCount="3" width="100"/> 

    <mx:HBox>
        <mx:Button label="&lt;-" click="button1_clickHandler(event)"/>
        <mx:Button label="->" click="button2_clickHandler(event)"/>
    </mx:HBox>
</mx:Application>
于 2012-05-02T13:30:13.750 回答
0

您正在创建一个包含一项 100 次的 ArrayList。用这个替换,你应该没问题:

var arr:ArrayList = new ArrayList();
for (var i:int = 0; i<100; i++) {
    arr.addItem(i);
} 

或者更好的是,只需将您的 XML 包装在 XMLListCollection 中,而不是一一复制节点(假设它是您想要的 XML 数据的实际内容而不是索引):

private function completeHandler(event:Event):void
{
    var xmlData:XML = XML(event.target.data);
    list.dataProvider = new XMLListCollection(xmlData.children());
}

(请注意,这不是 DataBinding:它只是设置dataProvider属性)

于 2012-04-27T11:19:49.270 回答