8 Replies Latest reply on Nov 18, 2010 6:01 PM by YuHsin4221123

    Retrieve return value from method

    YuHsin4221123

      I am doing a if else statement my flex component. this is how it looks like

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      xmlns:mx="library://ns.adobe.com/flex/mx"
                      autoDrawBackground="true" width="169" height="24">
          <s:layout>
              <s:HorizontalLayout/>
          </s:layout>
         
          <fx:Script>
              <![CDATA[
                 
                  [Bindable]
                  private function starRating():String{
                      if (data.rating == "1"){
                          return "images/1star.gif";
                      }
                      else if (data.rating == "2"){
                          return "images/2star.gif";
                      }
                      else if (data.rating == "3") {
                          return "images/3star.gif";
                      }
                      else if (data.rating == "4") {
                          return "images/4star.gif";
                      }
                      else {
                          return "images/5star.gif";
                      }
                  }
              ]]>
          </fx:Script>
         
          <s:Label text="{data.expertdetail}" x="1" y="7" height="23"/>
          <mx:Image x="119" y="-1" width="25" height="24" source="starRating()"/>
         
      </s:ItemRenderer>

       


      i wanted my image component to be dynamic. to get the image based on the data.rating value. I believe the problem lies on my source="" code.

       

      Could I know what is the exact way to fix this problem? Thanks

        • 1. Re: Retrieve return value from method
          drkstr_1 Level 4

          You cannot bind to a function, but you can bind to data and pass it through a function to return a result.

           

           

          
          source="{starRating(_data.raiting)}"
          
          [Bindable]
          private var _data:Object;
          
          private function starRating(raiting:String):String
          {
              if (rating == "1")
              {
                  return "images/1star.gif";
              }
              ...
          }
          
          • 2. Re: Retrieve return value from method
            YuHsin4221123 Level 1

            thanks for the reply. but I would like to know how can i retrieve the return value to the img component. i want my img component to display the return img.

            • 3. Re: Retrieve return value from method
              Arun Ganesh Level 3

                  <mx:Image x="119" y="-1" width="25" height="24" source="starRating()"/>

               

              starRating() - method is not bindable here

               

              try,

                  <mx:Image x="119" y="-1" width="25" height="24" source="{starRating()}"/>

              • 4. Re: Retrieve return value from method
                YuHsin4221123 Level 1

                thanks. I am able to get the retrieve data. but there an issue with my code

                 

                I am only able to get last loop "images/5star.gif"

                 

                I suspect that the problem lies on the data.rating, for this component it is actually a item renderer from list.

                • 5. Re: Retrieve return value from method
                  SashaKeith Adobe Employee

                  You can override the data setter. Something like this may work for you:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                                  xmlns:s="library://ns.adobe.com/flex/spark"
                                  xmlns:mx="library://ns.adobe.com/flex/mx"
                                  autoDrawBackground="true" width="169" height="24">
                      <s:layout>
                          <s:HorizontalLayout/>
                      </s:layout>
                     
                      <fx:Script>
                          <![CDATA[

                              override public function set data(value:Object):void
                              {
                                  super.data = value;
                                  if (value == null)
                                  {
                                      return;
                                  }
                                  lblExpertDetail.text = value.expertdetail ;


                                  if (value.rating == "1"){
                                      imgStarRating.source="images/1star.gif";
                                  }
                                  else if (value.rating == "2"){
                                      imgStarRating.source="images/2star.gif";
                                  }
                                  else if (value.rating == "3") {
                                      imgStarRating.source="images/3star.gif";
                                  }
                                  else if (value.rating == "4") {
                                      imgStarRating.source="images/4star.gif";
                                  }
                                  else {
                                      imgStarRating.source="images/5star.gif";
                                  }

                              }

                          ]]>

                   

                      </fx:Script>
                     
                      <s:Label id="lblExpertDetail" x="1" y="7" height="23"/>
                      <mx:Image id="imgStarRating" x="119" y="-1" width="25" height="24"/>
                     
                  </s:ItemRenderer>

                  • 6. Re: Retrieve return value from method
                    YuHsin4221123 Level 1

                    all my image are setted to the 5star.gif no matter what value i set. I believe the issue still lies on value.rating

                     

                    but lblExpertDetail.text = value.expertdetail; isn't encountering any issue from this code.

                    • 7. Re: Retrieve return value from method
                      SashaKeith Adobe Employee

                      Can you debug, set breakpoints before the if..else.. block and examine the rating value? Is it a number or int instead of a String by any chance? That should give you some clues maybe.

                      • 8. Re: Retrieve return value from method
                        YuHsin4221123 Level 1

                        yeah. i managed to fix the problem using the debugger, something is wrong with my getter and setter. thanks alot!