6 Replies Latest reply on Mar 27, 2014 5:57 AM by ibsenleo

    ItemRenderer fails updating values with a sort applied to the ArrayCollection (dataProvider)

    ibsenleo Level 1

      Hello everyone,

      Im updating the ArrayCollection that is the source of my list component

       

      (the email never changes)

       

      o:Object = {user:"username",email:"emailAdress",state:"number",imageURL="urlToImage"} //This comes from a function and arrives well.
      
      for (var j:int = 0; j < _arcUserList.length; j++) 
           {
                if(_arcUserList.getItemAt(j).email == o.email)
                 {
                      //_arcUserList.setItemAt(o,j) //I've tried this but doesn't work too!!
      
                    _arcUserList.getItemAt(j).user = o.user;
                    _arcUserList.getItemAt(j).email = o.email;
                    _arcUserList.getItemAt(j).state = o.state;
                    _arcUserList.getItemAt(j).imageURL = o.imageURL;
                    _arcUserList.itemUpdated(_arcUserList.getItemAt(j));
      
      
                }
            }
      
      

       

      This works well without the next sort settings, but if i add this lines in the init() it gives me:

       

      Error #1009: Cannot access a property or method of a null object reference (in the first line of the onChange function of Renderer in red below)

       

       

      var iSortByState:ISortField = new SortField("state",true);
      var iSortByName:ISortField = new SortField("user",false);
      var sort:Sort = new Sort();
      sort.fields = [iSortByState,iSortByName];
      _arcUserList.sort = sort;
      _arcUserList.refresh();
      
      

       

      here i post my item renderer

       

       

      <?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" 
                                              width="100%"
                                              autoDrawBackground="true" 
                                              creationComplete="fncInit(event)"
                                              dataChange="onChange(event)">
                <s:layout>
      
                          <s:HorizontalLayout gap="10" horizontalAlign="left"
                                                                            paddingTop="5" paddingBottom="5"
                                                                            paddingLeft="10" paddingRight="10"
                                                                            verticalAlign="middle"/>
                </s:layout>
      
                <s:Image id="_imgUser" height="32" width="32" />
                <s:VGroup width="70%">
                          <s:Label id="_lblUsername" fontSize="12" fontWeight="bold" />
                          <s:Label id="_lblState" color="#868686" fontSize="10" fontStyle="italic" /> 
                </s:VGroup>
                <s:HGroup width="100%"  horizontalAlign="right">
                          <s:Button width="40" label="@" click="onClickButton(event)" visible="false"/> 
                </s:HGroup>
      
      
      
                <fx:Script>
                          <![CDATA[
                                    import flash.filters.*;
      
                                    import mx.collections.ArrayList;
                                    import mx.controls.Alert;
                                    import mx.events.FlexEvent;
                                    import mx.events.StateChangeEvent; 
      
      
                                    [Bindable]private var arrStates:Array =["Disconnected","Busy","Available"];
      
                                    [Bindable]private var _nState:Number;
                                    [Bindable]private var _sImgURL:String;
                                    [Bindable]private var _sUser:String;
                                    [Bindable]private var _sEmail:String;
      
      
      
                                    protected function fncInit(event:FlexEvent):void
                                    {
                                              _nState = this.data.state;
                                              _sImgURL = this.data.imageURL;
                                              _sUser = this.data.user;
                                              _sEmail = this.data.email;
      
      
      
                                              this.setStyle("contentBackgroundColor","0xFF0000");
      
                                              _imgUser.smooth = true;
                                              fncLoadPicture(_sImgURL);
      
                                              _lblUsername.text = this.data.user;
                                              _lblState.text = arrStates[this.data.state];
      
                                    }
      
                                    private function fncLoadPicture(sURL:String):void{
                                              var context:LoaderContext = new LoaderContext();
                                              context.checkPolicyFile = true;
                                              context.applicationDomain = ApplicationDomain.currentDomain; 
                                              var fotoLoader:Loader = new Loader();
                                              fotoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
                                              fotoLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onImageError);
                                              fotoLoader.load(new URLRequest(sURL), context);
                                    }
      
                                    private function onImageLoaded(event:Event):void {
                                              var bmp:Bitmap;
                                              bmp = ( event.target as LoaderInfo ).content as Bitmap; 
                                              _imgUser.source = bmp;
      
      
                                    }
      
                                    private function onImageError(event:Event):void {
                                              Alert.show("Error loading imabe.", "Alert");
                                    }
      
      
      
                                    protected function onClickButton(event:MouseEvent):void
                                    {
                                              trace(data.user+": "+data.state); 
                                    }
      
      
                                    protected function onChange(event:FlexEvent):void
                                    {
      
                                              _lblUsername.text = this.data.user; //if i comment this, it fails in the next and so on.
                                              _lblState.text = arrStates[this.data.state];
                                              fncLoadPicture(this.data.imageURL); 
      
                                              var glow:GlowFilter = new GlowFilter();
                                              glow.alpha=0.8;
                                              glow.blurX=2;
                                              glow.blurY=2;
                                              glow.strength=30;
                                              glow.inner=false; 
                                              glow.quality=BitmapFilterQuality.HIGH;
      
                                              switch (this.data.state.toString())
                                              {
                                                        //Disconnected - Invisible
                                                        case "0":
                                                                  _lblUsername.setStyle("color",0x555555);
      
                                                                  glow.color=0x6E6E6E;
      
                                                                  var matrix:Array = new Array();
                                                                  matrix=matrix.concat([0.4,0.4,0.4,0,0]);// red
                                                                  matrix=matrix.concat([0.4,0.4,0.4,0,0]);// green
                                                                  matrix=matrix.concat([0.4,0.4,0.4,0,0]);// blue
                                                                  matrix=matrix.concat([0,0,0,0.5,0]);// alpha
                                                                  var cmf:ColorMatrixFilter=new ColorMatrixFilter(matrix);
                                                                  _imgUser.filters=[cmf,glow];
      
                                                        break;
      
                                                        //Busy
                                                        case "1":
                                                                  glow.color=0xDF0101;
                                                                  _imgUser.filters=[glow];
                                                                  break;
      
                                                        //Available
                                                        case "2":
      
                                                                  glow.color=0x01DF01;
                                                                  _imgUser.filters=[glow];
                                                                  break;
      
                                              }
      
      
                                    }
      
                          ]]>
                </fx:Script>
      
      
      
      </s:ItemRenderer>
      
      

       

      It Works well until i put the sort.

       

      Any suggestion?