5 Replies Latest reply on May 20, 2010 2:28 PM by nIvelyx

    Question about return variables in Flex

    nIvelyx Level 1

      Hello,

       

      I'm wondering how to properly handle this scenario.. any thoughts or tips will be appreciated:

       

      I have a function where I'm dynamically populating a bunch of checkboxes on my screen.

       

      The function loops through my ArrayCollection and then draws the checkboxes on the canvas.

       

      Each checkbox has a description for the label field, and the "id" coming from the database for it's data field

       

      For example:

       

      ...

      for (var i:int=1;i<myArrayCollection;i++) {

           var cb:Checkbox = new Checkbox;

      ...

           cb.label = myArrayCollection[i].data;

           cb.data = myArrayCollection[i].id;

      ...

      }

       

      This works fine and my checkboxes are showing up on the screen!

       

      I'm now trying to make it so that certain checkboxes are checked...

       

      To accomplish that, I wrote another function which takes 2 parameters getCheckboxes(key, integer)

       

      The get checkboxes function is pretty simple, it just checks aganist my database for a value and either returns true or false.. The event handler for the function looks like this:

       

       

                  public function evtGetCheckboxes(e:ResultEvent):Boolean {
                      var bGood:int = e.result as int;
                      var myBoolean:Boolean = new Boolean;
                     
                      if (bGood == 1) {
                          myBoolean = true;
                      } else {
                          myBoolean = false;
                      }
                      Alert.show(myBoolean.toString());
                      return myBoolean;
                  }

       

      The values that come from this function are correct. I get 'true' when I need things to be 'true' and false when I need them to be false. That's a good thing.

       

      The problem that I'm running into now is that when I try to run this function within my initial function, all of my checkboxes come back 'selected'.

       

      If we take the original code again:

       

      for (var i:int=1;i<myArrayCollection;i++) {

           var  cb:Checkbox = new Checkbox;

      ...

           cb.label =  myArrayCollection[i].data;

           cb.data = myArrayCollection[i].id;

       

        // ADDED this to figure out the status of the checkbox:

       

           cb.selected = javaRO.getCheckboxes(9999, i);

      ...

      }

       

      All of the checkboxes are getting selected, regardless of what's being returned through that get function call.

       

      What am I doing wrong here? The getCheckboxes function is returning the correct values (again, it returns true when it should be, and false when it shouldn't be... I checked it with my Alert.show(myBoolean.toString());, but the cb.selected is always coming through with every item checked instead of just certain ones.

       

      If anyone can assist me, it would be super helpful

       

      Thank you very much.

        • 1. Re: Question about return variables in Flex
          David_F57 Level 5

          hi,

           

           

          I would add the selected state to your array,

           

          cb.checked = false;

           

          so

           

          for (var i:int=1;i<myArrayCollection;i++) {

               var cb:Checkbox = new Checkbox;

          ...

               cb.label = myArrayCollection[i].data;

               cb.data = myArrayCollection[i].id;

               cb.checked = myArrayCollection[i].checked;    

          ...

          }

          Now when you test the checkbox you could update the array then when you need to update the table you just iterate through the array.

           

          David.

          1 person found this helpful
          • 2. Re: Question about return variables in Flex
            nIvelyx Level 1

            Hi David,

             

            Thanks for the quick reply. I did as you suggested and added the cb.selected = false, based on the arraycollection's checked property (which doesn't exist)... The issue still remains that I think I want to be able to dynamically figure out which boxes should be checked, based on the pivot table that I have in my database.

             

            Saved results for user 1 might be as follows:

             

            - Checkbox 1 (checked)

            - Checkbox 3 (checked)

            - Checkbox 90 (checked)

             

            and so thats what the getCheckboxes() function was doing.. it went out and fetched those results... that's why I was trying to use it within the code like

             

            cb.selected = javaRO.getCheckboxes(<user_id_key>, i);   where i would be the current checkbox item that's being iterated through the loop.

             

             

            If you have any other ideas (or if anyone else wants to chime in too), please feel free!!

             

            Thank you!

            • 3. Re: Question about return variables in Flex
              David_F57 Level 5

              hi,

               

              When you create the arraycollection (before iterating to create the checkboxes) can't you retrieve the check values and add them to the array then create the checkboxes ?

               

              Maybe I am missing what you are doing is it something like this

               

              private var cb_arr:ArrayCollection = new ArrayCollection();

               

              ........

              do something to populate cb_arr information

              .........

               

               

              private function createCheckBoxes():void

              {

              for (var i:int=1; i < cb_arr.length;i++)

              {

              var cb:CheckBox = new CheckBox();

              cb.x=10;

              cb.y=25*i+10;

              cb.selected = false;  <==== this could already be retrieved and placed into the arraycollection

              cb.label = cb_arr[i].label;

              cb_arr.mycheckbox = cb;

              cbCanvas.addChild(cb);

              }

              }

               

              another way to set the values

               

              private function setCheckBoxes(): void

              {

              for (var i:int=0; i <cb_arr.length;i++)

              {

              (cb_arr[i] as CheckBox).selected=myRetrievedResult.checked;

              }

              }

               

              David.

              • 4. Re: Question about return variables in Flex
                GordonSmith Level 4

                getCheckboxes() is an asynchronous call to a Java RemoteObject to fetch some data, which takes a while. This method doesn't return the true/false result that you want because it returns without waiting for the data to arrive. Therefore you can't use it to set cb.selected properly. The data arrives later and gets handled by your result handler. You would have to set cb.selected there. (BTW, an event handler doesn't return anything; it should have type void.)

                 

                But calling the server over and over to find out whether every checkbox should be selected is a bad design... it involves too many server calls. If the server is telling you the labels of all the checkboxes, it should also tell you their initial selection state.

                 

                BTW, you should also not be using 'new' with primitive types like Boolean.

                 

                Gordon Smith

                Adobe Flex SDK Team

                1 person found this helpful
                • 5. Re: Question about return variables in Flex
                  nIvelyx Level 1

                  Thank you both for all of your help. I really appreciate the responses and I was able to get this problem solved. Also,  I re-worked the logic a bit so that I'm no longer calling the server each time in order to get the checkbox status. I now simply do 2 calls. One call is to determine the checkboxes that should be checked, and the other call is to determine the actual list of checkboxes. The values are stored in separate arraycollections... this way I'm not continually pinging the server so that I (hopefully) wont run into any issues down the line.

                   

                  Thank you both for your support and help on this issue.

                   

                  Cheers