我试图通过使用一个名为TwoLabelCell
扩展的类创建两个标签标签字段来创建一个简单的两个标签列表,AlternatingCellRenderer
如下所示:
package views
{
import flash.text.TextFormat;
import qnx.fuse.ui.listClasses.AlternatingCellRenderer;
import qnx.fuse.ui.text.Label;
public class TwoLabelCell extends AlternatingCellRenderer {
private var description:Label;
private var labelFormat:TextFormat;
private var text:String;
public function TwoLabelCell(labelText:String) {
this.text = labelText;
}
override protected function init():void {
super.init();
labelFormat = new TextFormat();
labelFormat.color = 0x777777;
labelFormat.size = 17;
description = new Label();
description.x = 17;
description.y = 33;
description.format = labelFormat;
description.text = text;
this.addChild(description);
}
}
}
请记住,当时这只是一个测试,所以我只使用一个标签(在我弄清楚这个问题后,我将添加另一个并使用相同的逻辑)。这里的主要思想是,当我调用它时,它将设置text
变量,该变量将用于在创建列表时更改列表中标签的文本。现在,这里是主应用程序文件:
package {
import flash.display.Sprite;
import qnx.fuse.ui.events.ListEvent;
import qnx.fuse.ui.listClasses.List;
import qnx.fuse.ui.listClasses.ListSelectionMode;
import qnx.fuse.ui.listClasses.ScrollDirection;
import qnx.ui.data.DataProvider;
import views.TwoLabelCell;
[SWF(height="600", width="1024", frameRate="30", backgroundColor="#FFFFFF")]
public class PackTrack extends Sprite {
private var packList:List;
public function PackTrack() {
super();
initializeUI();
}
private function initializeUI():void {
packList = new List();
packList.setPosition(0, 0);
packList.width = 310;
packList.height = 400;
packList.rowHeight = 50;
packList.selectionMode = ListSelectionMode.SINGLE;
packList.scrollDirection = ScrollDirection.VERTICAL;
var arrMonth:Array=[];
arrMonth.push({label: "January"});
arrMonth.push({label: "February"});
arrMonth.push({label: "March"});
arrMonth.push({label: "April"});
arrMonth.push({label: "May"});
arrMonth.push({label: "June"});
arrMonth.push({label: "July"});
arrMonth.push({label: "August"});
arrMonth.push({label: "September"});
arrMonth.push({label: "October"});
arrMonth.push({label: "November"});
arrMonth.push({label: "December"});
packList.dataProvider = new DataProvider(arrMonth);
packList.cellRenderer = TwoLabelCell("Testing Label");
packList.addEventListener(ListEvent.ITEM_CLICKED, onListClick);
this.addChild(packList);
}
private function onListClick(event:ListEvent):void {
trace("Item clicked: " + event.data.label);
trace("Index clicked: " + event.index);
}
}
}
当我尝试运行时,我收到此错误:
TypeError: Error #1034: Type Coercion failed: cannot convert "Testing Label" to views.TwoLabelCell.
at PackTrack/initializeUI()[/Users/Nathan/Documents/Adobe Flash Builder 4.6/AIRTest/src/PackTrack.as:46]
at PackTrack()[/Users/Nathan/Documents/Adobe Flash Builder 4.6/AIRTest/src/PackTrack.as:19]
关于如何解决这个问题的任何想法?
PS:我正在学习 Flex(来自 Java)