2012-06-15 90 views
0

我試圖通過創建使用稱爲TwoLabelCell延伸AlternatingCellRenderer,看起來像這樣類中的兩個標籤,標籤字段創建一個簡單的兩個標記列表:參數傳遞到自定義CellRender

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未來)

回答

4

直接回答你的問題:你是因爲你想在packList.cellRenderer = TwoLabelCell("Testing Label")投一個字符串到TwoLabelCell得到一個classcast錯誤。所以我想你只是忘了new關鍵字。

我可以告訴你來自一個Java背景:該代碼看起來很輕快;)
所以我想我會告訴你我會怎麼做這個Flex的方式。我不知道你使用的這些黑莓課程,所以我必須堅持使用普通的Flex來展示它。

創建具有可綁定屬性的模型類:

public class Pack {  
    [Bindable] public var label:String; 
    [Bindable] public var description:String;  
} 

現在創建一個ItemRenderer類呈現在列表中的數據,讓我們把它PackRenderer.mxml

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       autoDrawBackground="true" 
       height="50"> 

    <s:layout> 
     <s:HorizontalLayout verticalAlign="middle" /> 
    </s:layout> 

    <s:Label id="labelDisplay" /> 
    <s:Label text="{data.description}" color="0x777777" fontSize="17" /> 

</s:ItemRenderer> 

通知我設置height50這與您使用的rowHeight具有相同的效果。 ItemRenderer的data屬性是它將表示的包模型實例。
帶有標識labelDisplay的標籤將自動獲取由Flex框架分配給其text屬性的值data.label。對於其他標籤,我使用數據綁定({})將模型description屬性綁定到標籤的text屬性。

這個itemRenderer的創建列表:

<fx:Script> 
    <![CDATA[ 
     protected function handleItemSelected():void { 
      trace("Item selected: " + list.selectedItem.label); 
      trace("Index selected: " + list.selectedIndex); 
     } 
    ]]> 
</fx:Script> 

<fx:Declarations> 
    <s:ArrayCollection id="dp"> 
     <so:Pack label="Item A" description="description A" /> 
     <so:Pack label="Item B" description="description B" /> 
     <so:Pack label="Item C" description="description C" /> 
    </s:ArrayCollection> 
</fx:Declarations> 

<s:List id="list" dataProvider="{dp}" width="310" height="400" 
     itemRenderer="net.riastar.PackItemRenderer" 
     change="handleItemSelected()" /> 

我創建的數據內嵌在這裏,當然這個ArrayCollection中也可以同樣來自服務器。
List的佈局默認是VerticalLayout,所以我沒有定義它。
Spark List沒有itemClick事件處理程序,所以我使用change處理程序,該處理程序在List的選定索引/項目更改時執行。

+0

難道只有使用ActionScript才能做到這一點嗎?因爲不喜歡在我的項目(Java的另一個壞習慣)混合語言,具有相同目的,也是因爲所有BlackBerry庫文檔都使用普通ActionScript,所以像我這樣的新手可以更容易地實現新事物。 **:)** –

+1

@NathanCampos在我開始使用Flex時,我曾經厭惡混合語言,但事實證明有幾種技術可以將它們乾淨地分開(我當然不能在評論中討論這些技術)。儘管如此,你可以在普通的ActionScript中做到這一點,但是我懷疑這對你來說會更容易:它會變得更加冗長,你必須更好地瞭解這個框架。作爲一個例子,使用綁定'text =「{data.description}」':在AS中,你必須像這樣'BindingUtils.bindProperty(myLabel,'text',data,'description')'這樣做。 – RIAstar

+1

@NathanCampos我剛剛寫了一個你可能會覺得有趣的答案,因爲它討論了從動作中分離mxml的這種技術(我的首選):http://stackoverflow.com/questions/11059302/custom-composite-control-not-渲染-正確地換僅-0-5-1仲是後/ 11061988#11061988 – RIAstar

相關問題