apache-flexflex-mobile

How to get a view to read data


I need to convert a lot of stuff in my profession - so I'm building a conversion tool for my phone with some of the conversions I use a lot.

Now, I want to be able to build this properly. So far, here's my code:

    <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="Length">

<fx:Script>
    <![CDATA[
        protected function button1_clickHandler(event:MouseEvent):void
        {
            var Result:String;
            var Finish:Number;
            var Start:Number = parseFloat(Input.text);
            Finish = Start * convert.selectedItem.data; 
            Result = String(Finish);
            answer.text = Result;

        }
    ]]>
</fx:Script>

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




<s:TextInput id="Input" x="20" y="46"/>
<s:SpinnerListContainer x="140" y="122" width="200" height="200">
    <s:SpinnerList id="convert" height="100%" width="100%" labelField="label" selectedIndex="1">
        <s:ArrayList>
            <fx:Object label="km to mi" data="0.62137119224"></fx:Object>
            <fx:Object label="km to yd" data="1093.6132983 "></fx:Object>
            <fx:Object label="km to ft" data="3280.839895"></fx:Object>
            <fx:Object label="km to in" data="39370.07874"></fx:Object>
            <fx:Object label="km to m" data="1000"></fx:Object>
            <fx:Object label="km to cm" data="100000"></fx:Object>
            <fx:Object label="km to mm" data="1000000"></fx:Object>

    </s:ArrayList>
            </s:SpinnerList>
        </s:SpinnerListContainer>
        <s:Label id="answer" x="66" y="533" width="348" text="Answer"/>
        <s:Button x="66" y="377" width="338"           click="button1_clickHandler(event)" label="Button"/>

    </View>

As you can see, I'm going to run into some problems with this:

1) Everything is hard-coded, and if I want to add, remove or change the elements in the array, it's going to be a bit of a pain.

2) I have a view that is essentially the same for my volume and weight conversions. With the same problem.

What I'd like to do, but I'm having some trouble understanding, is getting all that hard-coded stuff in one place and having the same view show it based on my previous view which is just a plain, hard-coded list.

I'm thinking of something like an xml sheet, and adding a category = "length" or category = "weight" element to the objects, so that I can show the category from the xml in the List, then when I click "length" it reads the label + data from this list. Is that a good solution? And how exactly do I get the selectedItem to remember which part of the xml list the view should be populated from?

Would it be better to have several xml files? But that still would mean I have to update a whole bunch of places when I need it.

Basically, I need assistance with:

So - Now the question is two-fold:

1) How to keep a connection to xml/db open across multiple views?

2) How to populate the end-view from information from the db?

Thanks for the advice and help.


Solution

  • Have you looked at using resource bundles? Or LSOs?

    When persisting Flex data, you have 4 main options.

    1. Relational Databases (seems like overkill here)
    2. XML (which you already seem comfortable with)
    3. Resource Bundles
    4. Local Shared Objects

    The LSO example, linked above (#4), gives a potential solution to your question:

    ... how exactly do I get the selectedItem to remember which part of the xml list the view should be populated from?

    snippet:

    public function initApp():void {
        mySO = SharedObject.getLocal("mydata");
        if (mySO.data.visitDate==null) {
           welcomeMessage = "Hello first-timer!"
        } else {
           welcomeMessage = "Welcome back. You last visited on " +
              getVisitDate();
        }
     }
    
    private function getVisitDate():Date {
       return mySO.data.visitDate;
    }
    
    private function storeDate():void {
       mySO.data.visitDate = new Date();
       mySO.flush();
    }
    
    private function deleteLSO():void {
       // Deletes the SharedObject from the client machine.
       // Next time they log in, they will be a 'first-timer'.
       mySO.clear();
    }
    

    I'd recommend using either a combination of XML and LSOs or resource bundles and LSOs--where your XML/RB stores non-changing, static data and your LSO keeps track of dynamic data (view settings, etc).