Jordan
Jordan

Reputation: 659

Problem accessing value of selected item in a list

I'm having a problem with getting the value of an item that the user selects in my mobile Flex application. When I select an item from the list I place that item into an ArrayCollection. But when I check the value (trace()), the value is [object Object] and I can't seem to access the actual value of the object. Here is what I am doing:

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);

                }

output from trace():

selected Players: [object Object]

Thank you very much for any advice and insight.

UPDATE: Here's the working code:

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

Upvotes: 0

Views: 1641

Answers (2)

Frank
Frank

Reputation: 800

i don't now the stucture of your ArrayCollection.

try this, the second trace in the click handler will return the property "Name" of the AC. Update this with your property 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>

If you need all Player Names, you need a loop. in this case, i can also help you.

BR Frank

Upvotes: 1

JeffryHouser
JeffryHouser

Reputation: 39408

Firist issue is that you are tracing out the array, not a specific item from the array. This was mentioned in the comments.

The second issue is that the elements of your ArrayCollection are most likely objects. When tracing an object it is converted to a string using the toString() method. If you don't implement a toString() method, you'll get the defaul, or [object Object].

You can implement a toString() method to see different output when the object is traced.

You could also run your code in debug mode and use it to view the variable to see what is actually in your collection.

Upvotes: 1

Related Questions