因此,我正在關注上下文中的關於由Michael Labriola,Jeff Tapper和Matthew Boles撰寫的Adobe Flex 4培訓教程。Flex - 在ArrayCollection中的光標搜索不起作用
我建立一個購物車類,臨危一的ShoppingCartItem對象(這僅僅是一個POAO)從MXML和通過這個公共功能把它添加到一個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.
另一件要注意的是,我使用的是一本書的Flex 4時,我使用的4.6.0 。 SDK,這是最近的Adobe發佈之前,它被贈予Apache。我不知道這是否重要。
下面是的ShoppingCartItem的代碼:
[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類中包含的產品POAO。它是我的購物的這樣的構造函數中完成:
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();
}
你可以顯示爲集合設置排序的代碼嗎? [documentation](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/IViewCursor.html#findFirst())表示,您排序的屬性必須位於「重新尋找。你認爲什麼樣的財產? Sort屬性是一個原始類型(String,int等)還是POAO,就像你的'Product'類一樣?嘗試排序時得到的空指針異常可能是線索,它會發生什麼類/行號? +1使用'IViewCursor'(似乎沒有人利用它們)。 –
根據要求編輯更多信息@SunilD。當我嘗試調用$ items中的ArrayCollection的刷新函數時,在遊標搜索的最後返回'NullPointerException'。 –