Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center / Flex Test Drive for Mobile /

Flex Test Drive for Mobile: Add application functionality

by Adobe

Adobe logo

Modified

19 June 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Builder Flex mobile

Video | Code | Tutorial | Links

Edit and delete data

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.
  • Download Flash Builder 4.5 Trial
  • Download the completed tutorial project (ZIP, 14.8 MB)

Code

DetailView.mxml

<?xml version="1.0" encoding="utf-8"?> <s:View …> <fx:Script> <![CDATA[ (…) protected function editBtn_clickHandler(event:MouseEvent):void { navigator.pushView(AddEditView,getEmployeesByIDResult.lastResult); } protected function deleteBtn_clickHandler(event:MouseEvent):void { deleteEmployeeResult.token = employeeService.deleteEmployee(data as int); } protected function deleteEmployeeResult_resultHandler(event:ResultEvent):void { navigator.popView(); } ]]> </fx:Script> <fx:Declarations> (…) <s:CallResponder id="deleteEmployeeResult" result="deleteEmployeeResult_resultHandler(event)"/> </fx:Declarations> <s:actionContent> <s:Button id="editBtn" click="editBtn_clickHandler(event)" …/> <s:Button id="deleteBtn" click="deleteBtn_clickHandler(event)" …/> </s:actionContent> (…) </s:View>

AddEditView.mxml

<?xml version="1.0" encoding="utf-8"?> <s:View title="Add Employee" add="view1_addHandler(event)" …> <fx:Script> <![CDATA[ (…) protected function saveBtn_clickHandler(event:MouseEvent):void { if(currentState=="Add") { createEmployeeResult.token = employeeService.createEmployee(employee); } else { updateEmployeeResult.token = employeeService.updateEmployee(employee); } } protected function createEmployeeResult_resultHandler(event:ResultEvent):void { navigator.replaceView(DetailView,event.result as int); } protected function view1_addHandler(event:FlexEvent):void { if(data==null) { currentState="Add"; } else { currentState="Edit"; employee=data as Employee; title="Edit Employee"; } } protected function updateEmployeeResult_resultHandler(event:ResultEvent):void { navigator.popView(); } ]]> </fx:Script> <s:states> <s:State name="Add"/> <s:State name="Edit"/> </s:states> <fx:Declarations> (…) <employeeservice:EmployeeService id="employeeService"/> <s:CallResponder id="updateEmployeeResult" result="updateEmployeeResult_resultHandler(event)"/> </fx:Declarations> (…) </s:View>

Tutorial

In the previous tutorial you created a view to add a new employee to the database. In this tutorial you will use the same view to update the data for an existing employee in the database. You will also add functionality to delete employees from the database.

Note: You can complete this tutorial for a Flash Builder project set up to use either your local or public server.

Step 1: Create a click handler for the Edit button.

In DetailView.mxml, create a click handler for the Edit button. Inside the handler, navigate to AddEditView and pass to it the data for the selected employee.

To navigate to the AddEditView, use the pushView() method of the ViewNavigator. Set the first argument to the view to navigate to, AddEditView, and the second argument to the data you want to "pass" to the view, which in this case is the data for the employee. The employee data is held in getEmployeesByIDResult.lastResult .

Your handler should appear as shown here:

protected function editBtn_clickHandler(event:MouseEvent):void { navigator.pushView(AddEditView,getEmployeesByIDResult.lastResult); }

The Edit button should now appear as shown here:

<s:Button id="editBtn" click="editBtn_clickHandler(event)" ... />

Step 2: Create Add and Edit states.

Return to Design mode for AddEditView.mxml and use the States view to create a new state called Edit based on the existing state, <State1>. Rename <State1> to Add (see Figure 1).

Create Edit and Add states.
Figure 1. Create Edit and Add states.

Take a look at the generated code. You will see the two states defined; the View component's states property is set equal to an array of State objects for which you assign names.

<s:states> <s:State name="Add"/> <s:State name="Edit"/> </s:states>

Step 3: When the view is added, set the appropriate state.

Create an add handler for the View object. Inside the handler, set the view's currentState property to Add if no data is passed to the view (its data property is null). If data is not null, set the currentState to Edit, employee to data , and the title to Edit Employee.

The code for the View object should appear as shown here:

<s:View title="Add Employee" add="view1_addHandler(event)" ...>

The handler should appear as shown here:

protected function view1_addHandler(event:FlexEvent):void { if(data==null) { currentState="Add"; } else { currentState="Edit"; employee=data as Employee; title ="Edit Employee"; } }

Run the application. Select an employee and then click the Edit button. The title of the view should be Edit Employee and the TextInput fields should be populated with the current values for that employee (see Figure 2).

Note: If you do not see any data, make sure the getEmployeesByID() operation in the Data/Services view is configured to have a return type of Employee.

See employee details in the AddEditView view.
Figure 2. See employee details in the AddEditView view.

Step 4: Update the data on the server.

In the Data/Services view, configure updateEmployee() to have an input of type Employee and a return type of void (if they are not already configured). In Design mode, drag the updateEmployee() operation and drop it on the Save button. Pass the employee object to the operation call. Inside the Save button click handler, use conditional logic to make the appropriate service call.

Your handler should appear as shown here:

protected function saveBtn_clickHandler(event:MouseEvent):void { if(currentState=="Add") { createEmployeeResult.token = employeeService.createEmployee(employee); } else { updateEmployeeResult.token = employeeService.updateEmployee(employee); } }

You should also have a new CallResponder object defined in the Declarations block:

<s:CallResponder id="updateEmployeeResult"/>

Step 5: When the data is saved, return to the DetailView.

Generate a result handler for the updateEmployeeResult CallResponder. Inside the handler, pop the eixisting view to return to the DetailView.

Your responder should appear as shown here:

<s:CallResponder id="updateEmployeeResult" result="updateEmployeeResult_resultHandler(event)"/>

The handler should appear as shown here:

protected function updateEmployeeResult_resultHandler(event:ResultEvent):void { navigator.popView(); }

Run the application and add a new employee. This should work as before. Select an employee and click the Edit button. The details for the selected employee should be displayed. Change the value for at least one field and click Save (see Figure 3). The updated employee details should be displayed.

Edit an employee record.
Figure 3. Edit an employee record.

Step 6: Add delete functionality.

In the Data/Services view, configure deleteEmployee() to have an input of type int and a return type of void (if they are not already configured). Return to Design mode for DetailView.mxml and drag the deleteEmployee() operation and drop it on the Delete button. Pass the data object (cast as an int) to the service call.

Your handler should appear as shown here:

protected function deleteBtn_clickHandler(event:MouseEvent):void { deleteEmployeeResult.token = employeeService.deleteEmployee(data as int); }

Your Delete button should appear as shown here:

<s:Button id="deleteBtn" click="deleteBtn_clickHandler(event)" ... />

You should have a new CallResponder object defined in the Declarations block:

<s:CallResponder id="deleteEmployeeResult"/>

Step 7: Return to the employee list when the employee is deleted.

Generate a result handler for the deleteEmployeeResult CallResponder. Inside the handler, pop off the existing view.

The CallResponder should appear as shown here:

<s:CallResponder id="deleteEmployeeResult" result="deleteEmployeeResult_resultHandler(event)"/>

Its result handler should appear as shown here:

protected function deleteEmployeeResult_resultHandler(event:ResultEvent):void { navigator.popView(); }

Run the application and select one of the new employees you added. Click the Delete button. You should return to the employee list and no longer see that employee displayed. That employee has been deleted from the database.

In this module you added application functionality. You added the ability for your application to send emails and texts and to make calls. You also performed all the data CRUD in a mobile Flex application: you created, read, updated, and deleted data from the database. In the next module, you learn how to debug and deploy the application.

Learn more

Refer to the following resources to learn more about this topic:

Documentation: Using Flash Builder 4.5

  • Adding View States and Transitions

Documentation: Using Flex 4.5

  • View states

Documentation: Developing mobile applications with Flash Builder and Flex

  • Define views in a mobile application

Documentation: Accessing Data with Flex

  • Building the client application

More resources

  • Flex Developer Center
  • Mobile and Devices Developer Center

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Flex Test Drive for Mobile: Add application functionality
  • Flex Test Drive for Mobile: Add application functionality
  • Flex Test Drive for Mobile: Debug and package the application
  • Flex Test Drive for Mobile: Debug and package the application
  • Flex Test Drive for Mobile: Build a mobile application in an hour
  • Flex Test Drive for Mobile: Build a mobile application in an hour
  • Flex Test Drive for Mobile: Build a mobile application in an hour
  • Flex Test Drive for Mobile: Add application functionality
  • Flex Test Drive for Mobile: Add application functionality
  • Flex Test Drive for Mobile: Test and deploy your code

Tutorials and samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex user forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement