Flex Blog

Search:
  • Home
  • Examples
    • Thumb

      Flex Examples

      Check out our Flex Examples!

    • Thumb

      Flash Builder Examples

      Check out our Flash Builder Examples!

    • Thumb

      AIR Examples

      Check out our AIR Examples!

    • Thumb

      Flex Mobile Examples

      Check out our Flex Mobile Examples!

    Adobe® Flex, Adobe® Flash Builder and Adobe® AIR are registered trademarks of Adobe Systems.
  • Components
    • Thumb

      WP Flex Contact Form

      Check out our WP Flex Contact Form!

    • Thumb

      Flash CountDown Plugin

      Check out our Flash CountDown Plugin!

    This is an overview of all our Flash/Flex based Components.
  • Jobs
  • Flex Books
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
  • iOS
Browse > Home / Examples / CheckBox in List using MobileIconItemRenderer for Flex Mobile

CheckBox in List using MobileIconItemRenderer for Flex Mobile

29 March 2011

Line Break

Author: Arjan (47 Articles) - Author Website

Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.


In my previous post I showed you how you can set a decoratorClass of a MobileIconItemRenderer depending on the data of the list item. The example made me wonder if it was possible to display a CheckBox in a MobileIconItemRenderer. Turns out it’s pretty easy to do so.

First lets create a new Flex Mobile project, I named it CheckboxInMobileIconItemRenderer. For this example we are going to store our data in a model class. By using a model class we can retain our data when we navigate to other views, if we don’t do that, the view and any data, variables that are in the view get trashed by the runtime.

Create a new package called model. After that, create a new ActionScript class in the model package. Call it AppModel.as. To make sure the application always refers to the same model instance, we are going to simulate singleton behaviour. I’m not going to explain this here, it’s quite easy to see how this works. Basically the class keeps track of a static instance variable and only instantiates this one time. After that, it keeps returning this instance when an instance is requested by using the getInstance() method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package model
{
    [Bindable]
    public class AppModel
    {
        import mx.collections.ArrayCollection;
       
        private static var _instance:AppModel;
       
        public var items:ArrayCollection;
       
        public function AppModel()
        {
        }
       
        public static function getInstance():AppModel
        {
            if( _instance == null )
            {
                _instance = new AppModel();
            }
            return _instance;
        }
    }
}

Now navigate to the Home view Flash Builder created for you in the views package, add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        title="Select items:" initialize="init()">
    <s:layout>
        <s:VerticalLayout gap="0"/>
    </s:layout>
    <fx:Script>
        <![CDATA[
           
            import model.AppModel;
            import mx.collections.ArrayCollection;
           
            [Bindable]
            private var appModel:AppModel;

            // Generate dummy data
            private function init():void
            {
                // Get model instance
                appModel = AppModel.getInstance();
               
                // If items is empty, create dummy data
                if(appModel.items == null)
                {
                    appModel.items = new ArrayCollection();
                    var obj:Object;
                    for( var i:int=0; i<10; i++ )
                    {
                        obj = new Object();
                        obj.Name = "List item " + i;
                        obj.Description = "Description " + i;
                        obj.Status = (i % 2 == 0 ? true : false);
                        appModel.items.addItem(obj);
                    }
                }
            }
           
        ]]>
    </fx:Script>
   
    <s:List id="myList" dataProvider="{appModel.items}" labelField="Name"
            width="100%" height="100%"/>
</s:View>

Create a new MXML component in the views package, name it CheckboxListItemRenderer and make sure its based on MobileIconItemRenderer. Add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="utf-8"?>
<s:MobileIconItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                          xmlns:s="library://ns.adobe.com/flex/spark"
                          messageField="Description" height="100"
                          initialize="init()">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import spark.components.Application;
            import spark.components.CheckBox;
            import spark.components.List;
           
            private var cb:CheckBox;
           
            private function init():void
            {
                if( !cb )
                {
                    cb = new CheckBox();
                    cb.addEventListener(MouseEvent.CLICK, handleClick);
                    cb.x = this.parent.width-70;
                    cb.y = 25;
                    this.addChild( cb );
                }  
            }
           
            private function handleClick(event:MouseEvent):void
            {
                super.data.Status = cb.selected;
            }
           
            override public function set data(value:Object):void
            {
                if(value != null)
                {
                    super.data = value;
                    cb.selected = value.Status;
                }
            }
        ]]>
    </fx:Script>

</s:MobileIconItemRenderer>

Navigate to the home view and set the itemRenderer of the List in the home view to be the one we just created:

1
2
<s:List id="myList" dataProvider="{appModel.items}" labelField="Name"
        width="100%" height="100%" itemRenderer="views.CheckboxListItemRenderer"/>

Test the application. You should see something like this:

Home View

Create a second view in the views package, call it SelectedItemsView. Add the following code:

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" title="You selected:">
    <s:layout>
        <s:VerticalLayout gap="0"/>
    </s:layout>
    <s:List id="selectedItems" dataProvider="{data}"
            width="100%" height="100%" labelField="Name"/>
    <s:Button width="100%" label="Previous" click="navigator.popView()"/>
</s:View>

Go back to the Home view and add the following button:

1
<s:Button width="100%" label="Next" click="next()"/>

Add the following method in the script section:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Navigate to Selected Items View
private function next():void
{
    // Get all selected items
    var selectedItems:ArrayCollection = new ArrayCollection();
    for each( var obj:Object in appModel.items )
    {
        if( obj.Status )
        {
            selectedItems.addItem(obj);
        }
    }
    // Navigate to the SelectedItemsView with selectedItems as data
    navigator.pushView( SelectedItemsView, selectedItems );
}

Now you can navigate between the two views. Notice that the application “remembers” the selected items when you navigate back and forth between views. If you keep the items ArrayCollection as a variable in the view, it gets destroyed when you navigate away from the view. Screens of the finished application:

Home View Finished
Selected Items View

Related posts:

  1. Data Dependent decoratorClass in MobileIconItemRenderer Example
  2. List Directory with AIR in Flex 4
  3. View Navigation in a Mobile Flex Application
  4. TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  5. Using Swipe Gestures in Mobile Flex Application (AIR for Android)

Written by Arjan · Filed Under Examples 

Was this post useful to you?

Please rate this post, follow us @ twitter, or link to this page from your website!

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.38 out of 5)
Loading ... Loading ...

8b39f939015cd431901aba018a22bd63delicious

Comments

7 Responses to “CheckBox in List using MobileIconItemRenderer for Flex Mobile”

  1. Boris on April 4th, 2011 8:14 am

    Just what I was looking for. Thank you very much!

  2. Gerry on April 24th, 2011 8:56 am

    Thanks for your article!
    One question: Why is it impossible to add a Label instead of a Checkbox
    using the same approach.
    Thanks for your reply

    Gerry

  3. kalpesh on May 16th, 2011 8:21 am

    Hi,
    thanks the same thing i was looking for
    i have used it and modified it according to my need, but now i am facing probleb like, i am using it to show Employee Name in label field and Emp Designation in message field.

    my requirement is like i want to send the EmployeeId of selected Employee to next page or view can be possible with this, can you please provide some solution for it

    Thanking you in advance

  4. Christine on December 8th, 2011 2:02 am

    Thank you, this is a perfect example for what I need to do and is one of the few examples I’ve found out there that basically works as is. I also appreciate that the example is a complete project. The only adjustment I had to make to make it work was change MobileIconItemRenderer to its new name IconItemRenderer.

  5. kaloseto on December 8th, 2011 11:53 pm

    Awesome post…had been looking for one of these ..thanks…

  6. me on December 16th, 2011 9:33 pm

    An alternative approach to achieve the same:

    http://forums.adobe.com/message/4088929#4088929

  7. Manish on August 10th, 2012 8:34 am

    Hi Arjan, This is very simple and easy to get example for adding checkbox in list. But can we do this with IconItemRenderer. Please enlighten me. Its like the above list but with icon. Actually i have to show Friends list with a button on the right for action like invite, or accept request..

Get Adobe Flash player

  • +1?

  • Support Flex Blog!

  • $ 13 raised
    • 2012/01/13 8:22 PM Russell Brown donated $ 3.00
    • 2011/10/31 4:43 PM Steve Dakin donated $ 5.00
    • 2011/05/11 3:37 PM Roelof Albers donated $ 5.00
  • Stay in touch!

  • Popular Tags

    • AdvancedDataGrid
    • AIR
    • ArrayCollection
    • baseColor
    • Button
    • CursorManager
    • DataGrid
    • Dynamic
    • Effects
    • File
    • FileStream
    • Flash Builder
    • Flash Builder 4
    • Flex 4
    • Flex Mobile
    • Framework
    • Icon
    • Image
    • itemRenderer
    • LinkBar
    • Mobile
    • PHP
    • ProgressBar
    • Repeater
    • Style
    • SWIZ
    • Timer
    • Tree
    • Twitter
    • ViewStack
  • Advertisements

  • Recent Posts

    • Spooky Frenzy – iPad Game
    • Fountain Example
    • Reading & Writing files in Adobe AIR
    • CheckBox in List using MobileIconItemRenderer for Flex Mobile
    • Data Dependent decoratorClass in MobileIconItemRenderer Example
    • Flex 4 Resize Effect Example
    • Jump to next field using the Focus Manager
    • Searching Data using a Class Example
    • Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
    • TabbedMobileApplication Example in Flex Mobile (AIR for Android)
  • Categories

    • Examples
    • Guest Poster
    • iOS
  • Archives

    • September 2011
    • July 2011
    • May 2011
    • March 2011
    • February 2011
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • March 2009
    • February 2009
  • Blogroll

    • Adobe Flex Jobs
    • NL for Business
  • Meta

    • Register
    • Log in
    • WordPress
    • XHTML

Copyright © 2010 Flex Blog · AdobeĀ® and AdobeĀ® Flex are registered trademarks of Adobe Systems.

WordPress Adobe Flex Adobe Flash Builder Adobe AIR Creative Commons License

  • Popular Posts

    • Progressbar in Datagrid Example 13 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 8 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 5 (5.00 out of 5)
    • List Directory with AIR in Flex 4 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex FlashVars in AS3 Example 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
    • Flex Dynamic Chart Example 4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (5.00 out of 5)