?
Solved

flex dynamic mxml component adding

Posted on 2010-08-20
1
Medium Priority
?
1,463 Views
Last Modified: 2012-05-10
Hello,

I'm attempting to add mxml components dynamically to a display list within a form and am having issues.  

The form is intended to allow users to conduct customer interviews.  After selecting the interview type from a combo box, questions are retrieved via web service (working) and then added to a question object (custom AS3 class ).
These objects are then added to an array collection.  Then I create another array collection and add my mxml question 'widget' and map it to one of the existing question objects. When I run the application and refresh the data provider of the display list (using custom item renderer), the components will display but not the mapped question object values (i.e. the question text).

How do I get the mapped question object values to display within the component?



Here is how I am building the list data provider:

                                //save web service result (XML) for local use
                        var xmlResult:XML=new XML(event.result);

                        //create array collection to hold question object values
                        var questionsAC:ArrayCollection=new ArrayCollection();

                        //loop through all questions in result
                        for each (var question:XML in xmlResult.Table)
                        {
                              //create question object
                              var tempQuestionAdt:InterviewQuestionsAdt=new InterviewQuestionsAdt;
                              
                              //load question object values from XML input
                              tempQuestionAdt.LoadFromXML(question);
                              
                              //add questions object to array collection for later use
                              questionsAC.addItem(tempQuestionAdt);
                        }
                        
                        //create array collection to hold mxml components
                        var displayQuestionsAC:ArrayCollection = new ArrayCollection();
                        
                        try
                        {
                  // loop through all questions in questions array and assign objects to mxml components
                              for (var x:int=0; x < questionsAC.length; x++)
                              {
                                    //create new mxml component
                                    var thisQuestionWidget:InterviewQuestionWidget=new interviewQuestionWidget();
      
                                    //remap array collection object to custom object class
                                               
                                               
                                    //var tempQ:InterviewQuestionsAdt=questionsAC[x] as InterviewQuestionsAdt;

                                    //define a unique 'id' value for each created mxml component
                                    thisQuestionWidget.id="wgtQuestion" + x.toString();
                                    
                                    //set current question object to mxml component value
                                    thisQuestionWidget.QuestionAdt=tempQ;
                                    
                                    //Alert.show(tempQ.question);
                                    //thisQuestion.Activate();
                                    //thisQuestion.invalidateDisplayList();
                                    
                                    //add mxml component to display list array collection
                                    displayQuestionsAC.addItem(thisQuestion);
                              }
                              
                              //reset display list data provider
                              listQuestions.dataProvider=displayQuestionsAC;
                              listQuestions.invalidateDisplayList();
                        }
                        catch (err:Error)
                        {
                              //display error
                              Alert.show(err.message);
                        }



Here is the mxml List:

<mx:List x="10" y="184" width="698" height="414" id="listQuestions"  >
                  <mx:itemRenderer>
                                                
                                          <mx:Component>
                                                <ns2:InterviewQuestionOuterWidget  >
                                                
                                                </ns2:InterviewQuestionOuterWidget>
                                          </mx:Component>

                                    </mx:itemRenderer>
                  
                  </mx:List>



Here is the mxml component in question:


<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
		   width="524"
		   height="170"
		   cornerRadius="8"
		   borderThickness="1"
		   borderColor="#000000"
		   color="#000000"
		   borderStyle="solid"
		   backgroundColor="#B2B2B2"
		   fontFamily="futura md bt"
		   fontSize="10"
		   maxHeight="170"
		   maxWidth="524"
		   creationComplete="Activate();"	  
		   >
			   

	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import com.adobe.linguistics.spelling.SpellUI;
			import com.lag.adt.cosulting.interview.InterviewQuestionsAdt;
			import mx.controls.Alert;

			[Bindable]
			public var QuestionAdt:InterviewQuestionsAdt;
			
			public var cbAnswerScores:ArrayCollection = new ArrayCollection([{label:"Very Positive",score:5},{label:"Positive",score:4},{label:"Neutral",score:3},{label:"Negative",score:2},{label:"Very Negative",score:1}]);
			
			public var InitDone:Boolean = false;
			
			public function Init():void
			{
				InitDone = true;
			}
			
			public function Activate():void
			{
				Alert.show(QuestionAdt.question);
				trace("Question is:"+QuestionAdt.question);
				trace("Tip is: "+QuestionAdt.questiontips);
				txtQuestionText.text = QuestionAdt.question;
				txtQuestionText.toolTip = QuestionAdt.questiontips;
			}
			public function setQuestionText():void
			{
				 if(txtQuestionText != null)
				{ 
					Alert.show(QuestionAdt.question);
					txtQuestionText.text = this.QuestionAdt.question.toString();
					txtQuestionText.toolTip = this.QuestionAdt.questiontips.toString();
				}
			}
		]]>
	</mx:Script>


	<mx:TextInput x="10"
				  y="72"
				  width="458"
				  height="86"
				  fontFamily="futura md bt"
				  fontSize="10"
				  text="Answer Text Goes Here."
				  id="txtAnswerText"
				  creationComplete="SpellUI.enableSpelling(txtAnswerText, 'en_US');"/>
	<mx:TextInput x="10"
				  y="22"
				  width="376"
				  height="42"
				  editable="true"
				  backgroundColor="#E2DEDE" 
				  fontFamily="futura md bt"
				  fontSize="12"
				  id="txtQuestionText"
				  enabled="true" visible="true"
				  />
				  
	<mx:Button x="476"
			   y="72"
			   width="35"
			   height="86"
			   id="btnExpandAnswerText">
		<mx:downIcon>@Embed(source='../../Assets/img/up_arrow_16_x_16.gif')</mx:downIcon>
		<mx:icon>@Embed(source='../../Assets/img/down_arrow_16_x_16.gif')</mx:icon>
	</mx:Button>
	<mx:ComboBox x="394" y="24" width="118" id="cbAnswerScore" dataProvider="{cbAnswerScores}" labelField="label" selectedIndex="2" ></mx:ComboBox>
	<mx:Label x="394" y="54" id="lbQnum"/>

</mx:Canvas>

Open in new window

0
Comment
Question by:rwhitlatch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
jth_92 earned 2000 total points
ID: 33487384
Depending on the way your service is returning the xml there could be different methods.
What I would do is create a variable that is bindable in your mxml component for each property of the question that you need:

[Bindable] public var QuestionTextVar:String;
....
In the txtQuestionText TextInput Component add the property:
   text="{QuestionTextVar}" to show

<mx:TextInput x="10"
                          y="22"
                          width="376"
                          height="42"
                          editable="true"
                          backgroundColor="#E2DEDE"
                          fontFamily="futura md bt"
                          fontSize="12"
                          id="txtQuestionText"
                          enabled="true" visible="true"
                          text="{QuestionTextVar}" />                            


When you create the object from you main application:

                              //create question object
                              var tempQuestionAdt:InterviewQuestionsAdt=new InterviewQuestionsAdt;

                              /* replace this with ******************************
                              //load question object values from XML input
                              tempQuestionAdt.LoadFromXML(question);
                              ****************************************************/
                             /* with the following ***********************/
                            tempQuestionAdt.QuestionTextVar = question.lastResult.xmlQuestionTag //replace xmlQuestion Tag with the correct xml tag that returns the question

                              //add questions object to array collection for later use
                              questionsAC.addItem(tempQuestionAdt);

Thus, the mxml component should correctly load from the parent application.
For more info check out: http://livedocs.adobe.com/flex/3/html/help.html?content=layouts_12.html
where it discusses how to pass data to and from a pop-up window.
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question