user723858
user723858

Reputation: 1017

Push a View by converting a string to a class?

I am currently working in Flashbuilder 4.6 creating a mobile application and I wish to push the users to a view by based on the menu item they select. My code below shows how I have tried to convert a string from a selected list item into a class object and then pass that into the parameter for the pushView:

<fx:Script>
    <![CDATA[
        import mx.collections.*;            
        import spark.components.SplitViewNavigator;
        import spark.components.ViewNavigator;
        import spark.events.IndexChangeEvent;
        import spark.events.ViewNavigatorEvent;
        import spark.managers.PersistenceManager;
        import spark.skins.mobile.SplitViewNavigatorSkin;

        public var menuAC:ArrayCollection;          
        public var j_array:Array = [{label:"Home", data:"NLView"}, 
            {label:"All Jobs", data:"JList"}, 
            {label:"Company Profiles", data:"SView"}, 
            {label:"Search By Sector", data:"JView"}];


        protected function list_changeHandler(event:IndexChangeEvent):void
        {
            var selectedItem:String = new String(list.selectedItem.data);
            //trace("Selected Item = " + selectedItem);
            var splitNavigator:SplitViewNavigator = navigator.parentNavigator as SplitViewNavigator;
            var detailNavigator:ViewNavigator = splitNavigator.getViewNavigatorAt(0) as ViewNavigator;
            splitNavigator.hideViewNavigatorPopUp();

            if(list.selectedItem.data == "NewListView")
            {
                var myClass:Class = getDefinitionByName(selectedItem) as Class;                 
                trace("View Class = " + myClass);
                detailNavigator.pushView(myClass);
            }
            else if(list.selectedItem.data)
            {

            }


        }

        protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
        {

            if(data.view == "JSFilt")
            {
                menuAC = new ArrayCollection(j_array);  
            }

            list.dataProvider = menuAC;
            // TODO Auto-generated method stub
            var splitNavigator:SplitViewNavigator = navigator.parentNavigator as SplitViewNavigator;
            var detailNavigator:ViewNavigator = splitNavigator.getViewNavigatorAt(1) as ViewNavigator;
            splitNavigator.hideViewNavigatorPopUp();
        }


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

<s:BitmapImage width="100%" height="100%" source="@Embed('assets/images/sidebg.png')" fillMode="repeat"/>
<s:BitmapImage horizontalCenter="0" top="5" width="175" height="130" source="@Embed('assets/images/G_S_Logo_170x130.png')"/>

<s:List id="list" left="0" right="0"
        color="0xFFFFFF" contentBackgroundColor="0x101010"
        fontSize="20" width="280"
        alternatingItemColors="[0x202020,0x2a2a2a]"
        top="140" horizontalCenter="0" bottom="200"
        downColor="0x70B2EE"
        selectionColor="0x70B2EE"
        change="list_changeHandler(event)"> 
    <s:itemRenderer>
        <fx:Component>
            <s:IconItemRenderer messageStyleName="myFontStyle" fontSize="22" height="70" 
                                labelField="label"
                                messageField="" 
                                />
        </fx:Component>
    </s:itemRenderer>       
</s:List>

I have also tried using the following command which also did not work:

var viewClass:Class = Class(getDefinitionByName(getQualifiedClassName(selectedItem)));

Has anyone got anyidea how to look at a value selected on a list and then use that string name to push the App to the view as the pushView method wont accept a string??


Thanks for the suggestions and I like the idea of adding the class to the array and then passing that to the pushView. I had tried that in the past and it didnt work so I tried your method with the code below:

public var jobs_array:Array = [{label:"Home", data:"NewListView", class:NewListView},
                {label:"All", data:"JList"}, 
                {label:"C Prof's", data:"SView"}, 
                {label:"S By S", data:"JSView"}];

As you can see I have tried to implement what you suggested on the first line and I get the following errors:

1084: Syntax error: expecting colon before rightbrace.
1084: Syntax error: expecting identifier before class.
1084: Syntax error: expecting identifier before rightbrace.

This must a simple syntax thing but I cant see it, can someone please help me with this?

Upvotes: 1

Views: 1280

Answers (4)

sss
sss

Reputation: 21

Yes.. here is the answer for binding spark viewstack slelected child using a string var str:String="roof"; viewstack.selectedChild=NavigatorContent(viewstack.getChildByName(str));

Upvotes: 0

sss
sss

Reputation: 21

var imageClass:Class=getDefinitionByName("com.robaldred.composers."+imgclass) as Class;

Please add the packages name like this: com.robaldred.composers.

Upvotes: 2

Sunil D.
Sunil D.

Reputation: 18193

Why don't you just put a reference to the Class itself in the data, and don't worry about using a String to represent it.

Your data items would look like this:

var data:Array = [
{ label: "Item 1", class: ClassToUseForItem1 },
{ label: "Item 2", class: ClassToUseForItem2 },
{ label: "Item 3", class: ClassToUseForItem3 } ];

And now you can do pushView(list.selectedItem.class)

Upvotes: 0

Sam DeHaan
Sam DeHaan

Reputation: 10325

getDefinitionByName(selectedItem) is almost what you want, but not quite. See, selectedItem is one of the items from your array, say {label:"All Jobs", data:"JList"}, not just "JList".

getDefinitionByName(getQualifiedClassName(selectedItem)) is definitely not what you want, as getQualifiedClassName will return the fully qualified class name of selectedItem. In this case, that would be Object. Then getDefinitionByName will return the Object class.


getDefinitionByName(selectedItem.data) should work for you just fine. selectedItem.data should be JList, for example, and then getDefinitionByName should return the JList Class object.

Upvotes: 0

Related Questions