列表中的DataBinding

时间:2012-04-27 11:09:18

标签: flex flash-builder

我正在尝试将ArrayList中的数据绑定到列表,但是这里只有最后一个元素出现在列表中(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;
        }

我无法弄清楚该做什么?

2 个答案:

答案 0 :(得分: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属性)

答案 1 :(得分: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>