访问列表中所选项目的值时出现问题

时间:2011-09-02 12:58:43

标签: flex actionscript-3 arraycollection flex-mobile

我在获取用户在移动Flex应用程序中选择的项目的值时遇到问题。当我从列表中选择一个项目时,我将该项目放入ArrayCollection中。但是当我检查值(trace())时,值是[object Object],我似乎无法访问对象的实际值。这就是我在做的事情:

private var selectedPlayers:ArrayCollection = new ArrayCollection();
            private var numOfPlayers:int;
...

//check if item is not already in selected players list
                if(!selectedPlayers.contains(playerList.selectedItem))
                {

                    //add the selected item to the selected players list
                    selectedPlayers.addItem(playerList.selectedItem);
                    numOfPlayers++;
                    trace("selected Players: " + selectedPlayers);

                }

trace()的输出:

选定的玩家:[对象对象]

非常感谢您的任何建议和见解。

更新:这是工作代码:

[Bindable]
            public static var selectedPlayers:ArrayCollection = new ArrayCollection([
                {Name: "testname" }]);

...

//check if item is not already in selected players list
                if(!selectedPlayers.contains(playerList.selectedItem.PName))
                {
                    //add the selected item to the selected players list
                    selectedPlayers.addItem({Name: playerList.selectedItem.PName});
                    numOfPlayers++;
                }

2 个答案:

答案 0 :(得分:1)

Firist问题是你正在追踪数组,而不是数组中的特定项。评论中提到了这一点。

第二个问题是ArrayCollection的元素很可能是对象。跟踪对象时,使用toString()方法将其转换为字符串。如果你没有实现toString()方法,你将得到默认值,或[object Object]

您可以实现toString()方法,以在跟踪对象时查看不同的输出。

您还可以在调试模式下运行代码,并使用它来查看变量以查看集合中的实际内容。

答案 1 :(得分:1)

我现在不是ArrayCollection的结构。

试试这个,点击处理程序中的第二个跟踪将返回AC的属性“Name”。使用您的媒体资源名称进行更新。

<?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"
               initialize="application1_initializeHandler(event)">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            private var selectedPlayers:ArrayCollection = new ArrayCollection([
                {Name:"iTunes", id:"1"}, 
                {Name:"MediaPlayer", id:"2"},
                {Name:"WinAmp", id:"3"}]) 

            protected function button2_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                trace ("selectedPlayers: " + selectedPlayers)
                trace (selectedPlayers.getItemAt(0).Name);

            }
        ]]>
    </fx:Script>


    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>



    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            protected function application1_initializeHandler(event:FlexEvent):void
            {
                // TODO Auto-generated method stub

            }



        ]]>
    </fx:Script>

    <s:Button label="trace" click="button2_clickHandler(event)" />

</s:Application>

如果您需要所有玩家名称,则需要循环。在这种情况下,我也可以帮助你。

BR 弗兰克

相关问题