2011-09-02 68 views
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); 

       } 

輸出():

選擇玩家:[對象的對象]

非常感謝您的任何意見和見解。

更新:這裏的工作代碼:

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

你跟蹤整個ArrayCollection的,沒有任何具體的項目,從內它。 – shanethehat

+0

即使我跟蹤(「選定的玩家:」+ selectedPlayers.getItemAt(0))我得到相同的輸出。 – Jordan

回答

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 弗蘭克

+0

完美的作品。但是,如何將我的selectedPlayers ArrayCollection設置爲空白屬性?例如。名稱:-playerList.selectedItem- – Jordan

+0

更新:從不知道我想通了。非常感謝! – Jordan

+0

歡迎您,快樂編碼:-) – Frank

1

一下第的問題是,你正在追查出數組,從數組不是一個具體的項目。評論中提到了這一點。

第二個問題是ArrayCollection的元素很可能是對象。在跟蹤一個對象時,它使用toString()方法轉換爲一個字符串。如果你沒有實現一個toString()方法,你會得到這個默認值,或者[object Object]

您可以實現toString()方法來查看跟蹤對象時的不同輸出。

您也可以在調試模式下運行您的代碼並使用它來查看變量以查看您的集合中實際存在的內容。

相關問題