0

我在获取用户在我的移动 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);

                }

跟踪()的输出:

选定的玩家:[object Object]

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

更新:这是工作代码:

[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++;
                }
4

2 回答 2

1

我现在不知道你的 ArrayCollection 的结构。

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

<?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弗兰克

于 2011-09-02T13:52:07.403 回答
1

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

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

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

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

于 2011-09-02T13:46:18.240 回答