2013-08-05 8 views
0

그래서 저는 Michael Labriola, Jeff Tapper 및 Matthew Boles의 출처 인 Adobe Flex 4 교육 자료를 컨텍스트에 따라 검토하고 있습니다.Flex - ArrayCollection의 커서 검색이 작동하지 않습니다.

나는 MXML에서 (단지 POAO 인)는 ShoppingCartItem 개체를 받으면에이 공공 기능을 통해있는 ArrayCollection에 추가 쇼핑 카트 클래스 짓고 있어요 : 책에 따르면

private var $items:ArrayCollection = new ArrayCollection(); 
public function addItem(item:ShoppingCartItem):void 
    { 
     var inCart:Boolean = false; 
     var currentItem:ShoppingCartItem; 
     for(var i:int = 0; i < $items.length; i++) 
     { 
      currentItem = $items.getItemAt(i) as ShoppingCartItem; 
      if(item.$product == currentItem.$product) 
      { 
       inCart = true; 
       break; 
      } 
     } 
     if(inCart) 
     { 
      currentItem.$quantity++; 
     } 
     else 
     { 
      $items.addItem(item); 
     } 
     updateTotal(); 
     $items.refresh(); 
    } 

을, 이와 같은 IViewCursor를 사용하여 동일한 기능을 수행 할 수 있습니다.

public function addItem(item:ShoppingCartItem):void 
{ 
    var cursor:IViewCursor = $items.createCursor(); 
    var inCart:Boolean = cursor.findFirst(item); 
    if(inCart) 
    { 
     var existing:ShoppingCartItem = cursor.current as ShoppingCartItem; 
     existing.$quantity++; 
    } 
    else 
    { 
     $items.addItem(item) 
    } 
} 

문제는이 기능을 사용할 때 항목 수량이 업데이트되지 않는다는 것입니다. 그런 다음 2 개의 제품 중 1 개 항목을 가져야 할 때 1 개의 제품에 2 개의 항목이있는 장바구니가 있습니다. inCart 부울을 추적하면 내가하는 일에 상관없이 "false"가됩니다. 첫 번째 함수는 올바르게 작동하고 예상대로 작동하므로 데이터가 올바르게 업데이트되지 않는 이유는 알 수 없습니다. 또한 $items.refresh();을 정렬 (정렬) 끝에 호출하면 NullPointerException error.

또 하나주의해야 할 점은 4.6.0을 사용할 때 Flex 4 용 책을 사용하고 있다는 것입니다. . SDK는 Apache 이전의 마지막 Adobe 릴리스입니다. 이것이 중요한지 나는 모른다.

[Bindable] 
public class ShoppingCartItem 
{ 
    public var $product:Product; 
    public var $quantity:uint; 
    public var $subtotal:Number; 
    public function getSubTotal():Number 
    { 
     calculateSubTotal(); 
     return $subtotal; 
    } 
    public function toString():String 
    { 
     return "[ShoppingCartItem]"+$product.prodName+":"+$quantity; 
    } 
    public function calculateSubTotal():void 
    { 
     this.$subtotal = $product.listPrice*$quantity; 
    } 
    public function squeak():void 
    { 
     trace("squeak"); 
    } 
    public function ShoppingCartItem(product:Product, quantity:uint = 1) 
    { 
     this.$product = product; 
     this.$quantity = quantity; 
     calculateSubTotal(); 
    } 

편집 : 선일 D.

Product.as 클래스에 의해 더 많은 정보 요청이 :

[Bindable] 
public class Product 
{ 
    public var catID:Number; 
    public var prodName:String; 
    public var unitID:Number; 
    public var cost:Number; 
    public var listPrice:Number; 
    public var description:String; 
    public var isOrganic:Boolean; 
    public var isLowFat:Boolean; 
    public var imageName:String; 
    public function toString():String 
    { 
     return "[Product]"+this.prodName; 
    } 
    public static function buildProductFromAttributes(data:XML):Product 
    { 
     var p:Product; 
     var isOrganic:Boolean = ([email protected] == "Yes"); 
     var isLowFat:Boolean = ([email protected] == "Yes"); 
     p = new Product([email protected], 
         [email protected], 
         [email protected], 
         [email protected], 
         [email protected], 
         [email protected], 
         isOrganic, 
         isLowFat, 
         [email protected]); 
     return p; 
    } 
    public static function buildProduct(o:Object):Product 
    { 
     var p:Product; 
     p = new Product(o.catId,o.prodName,o.unitID,o.cost,o.listPrice, 
         o.description,(o.isOrganic == 'true'), 
         (o.isLowFat == 'true'),o.imageName); 

     return p; 
    } 
    public function Product(cid:Number, name:String, uid:Number, cost:Number, listp:Number, desc:String, iso:Boolean, ilf:Boolean, imn:String) 
    { 
     this.catID = cid; 
     this.prodName = name; 
     this.unitID = uid; 
     this.cost = cost; 
     this.listPrice = listp; 
     this.description = desc; 
     this.isOrganic = iso; 
     this.isLowFat = ilf; 
     this.imageName = imn; 
    } 
} 

ArrayCollection에 정렬하는 SortField가 여기있다

는 ShoppingCartItem에 대한 코드입니다 ShoppingCartItem 클래스에 포함 된 POAO 제품. 이 같은 shoppingCart가의 생성자 함수 내에서 이루어집니다 : 항목 (ShoppingCartItem)는 $ 항목 컬렉션이 아니므로 viewcursor 접근 방식이 작동하지

public class ShoppingCart 
{ 
    [Bindable] 
    private var $items:ArrayCollection = new ArrayCollection(); 
    public function ShoppingCart() 
    { 
     var prodSort:Sort = new Sort(); 
     var sortField:SortField = new SortField("product"); 
     prodSort.fields =[sortField]; 
     $items.sort = prodSort; 
     $items.refresh(); 
    } 
+1

컬렉션에 대한 정렬을 설정하는 코드를 표시 할 수 있습니까? [documentation] (http://help.adobe.com/ko_KR/FlashPlatform/reference/actionscript/3/mx/collections/IViewCursor.html#findFirst())는 정렬하려는 속성이 ' 다시 검색. 어떤 속성을 정렬합니까? sort 속성은'Product' 클래스와 같은 기본 유형 (String, int 등) 또는 POAO입니까? 당신이 정렬하려고 할 때 null 포인터 예외가 아마 실마리, 어떤 Class/line 번호가 발생합니까? +1 (IViewCursor)을 사용하면 아무도 사용할 수 없습니다. –

+0

@SunilD 요청한대로 자세한 정보로 편집 됨. $ item에있는 ArrayCollection의 새로 고침 기능을 호출하려고 할 때 커서 검색의 맨 마지막에서'NullPointerException'이 반환됩니다. –

답변

-1

이유입니다. 이 방법은 객체 참조를 비교하므로 ShoppingCartItem의 다른 인스턴스 인 경우 항목을 찾지 않습니다.

첫 번째 접근 방식에서는 ShoppingCartItem의 개체 참조를 비교하지 않고 항목의 "제품"속성을 비교합니다.