这是来自Flex移动项目的一些Flex代码。我想将Rss Feed加载到具有错误空格的IList中。到目前为止,我知道 Stringutil.trim(String)。但我陷入了应该使用它来使列表工作的地方。请指教。
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:studierende="services.studierende.*"
xmlns:allgemein="services.allgemein.*"
xmlns:veranstaltung="services.veranstaltung.*"
xmlns:wissenschaft="services.wissenschaft.*"
overlayControls="false">
<fx:Script>
<![CDATA[
import flash.net.dns.AAAARecord;
import mx.events.FlexEvent;
import mx.utils.StringUtil;
protected function openmenu(event:MouseEvent):void
{}
protected function reload(event:MouseEvent):void
{
asynclist.removeAll();
getDataResult.token = feed1.getData();
}
protected function loadRSS(event:FlexEvent):void
{
getDataResult.token = feed1.getData();
//I guess here is a good place to trim the whitespaces. But how ?
}
protected function loadfeed(event:MouseEvent,rid:Number):void
{
var rssid:Number = rid;
switch(rssid)
{
case 0:
asynclist.removeAll();
getDataResult.token = feed1.getData();
break;
case 1:
asynclist.removeAll();
getDataResult.token = feed2.getData();
break;
case 2:
asynclist.removeAll();
getDataResult.token = feed3.getData();
break;
case 3:
asynclist.removeAll();
getDataResult.token = feed4.getData();
break;
}
}
]]>
</fx:Script>
<fx:Declarations>
<s:CallResponder id="getDataResult"/>
<allgemein:Allgemein id="feed1"/>
<studierende:Studierende id="feed2"/>
<veranstaltung:Veranstaltung id="feed3"/>
<wissenschaft:Wissenschaft id="feed4"/>
</fx:Declarations>
<s:navigationContent>
<s:Button id="menubtn" label="Menu" click="openmenu(event)"/>
</s:navigationContent>
<s:actionContent>
<s:Button id="reloadbtn" label="RELOAD" click="reload(event)"/>
</s:actionContent>
<s:List id="list" left="10" right="10" top="50" bottom="10" creationComplete="loadRSS(event)"
itemRenderer="icItemRender" labelField="title">
<s:AsyncListView id="asynclist" list="{getDataResult.lastResult}" />
</s:List>
<s:HGroup x="10" y="10" width="748" height="32">
<s:Button height="32" label="Allgemein" fontSize="12" click="loadfeed(event,0)"/>
<s:Button height="32" label="Studierende" fontSize="12" click="loadfeed(event,1)"/>
<s:Button height="32" label="Veranstaltung" fontSize="12" click="loadfeed(event,2)"/>
<s:Button height="32" label="Wissenschaft" fontSize="12" click="loadfeed(event,3)"/>
</s:HGroup>
答案 0 :(得分:0)
我假设您想要从列表的每个元素中显示的标题中修剪空白。
最简单的方法可能是使用labelFunction。将其设置在列表中,如下所示:
<s:List id="list" left="10" right="10" top="50" bottom="10" creationComplete="loadRSS(event)"
itemRenderer="icItemRender" labelFunction="myLabelFunction">
注意到,我删除了你的labelField声明。该功能应该是这样的:
<fx:Script>
<![CDATA[
private function myLabelFunction(item:Object):String {
return Stringutil.trim(item.title);
}
]]>
</fx:Script>
您还可以创建自定义itemRenderer以获得相同的最终结果。
答案 1 :(得分:0)
我通过自定义项目渲染器
在flextras的帮助下解决了问题package
{
import com.adobe.fiber.styles.Style;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import mx.utils.StringUtil;
import spark.components.Button;
import spark.components.IconItemRenderer;
import spark.components.Image;
import spark.components.LabelItemRenderer;
import spark.components.supportClasses.StyleableTextField;
/**
*
* ASDoc comments for this item renderer class
*
*/
public class asListItemRenderer extends IconItemRenderer
{
private var titleDisplay:StyleableTextField;
private var msgDisplay:StyleableTextField;
private var link:String;
private var readon:Image;
private var stagewebview:StageWebView;
private var warning:AlertPopUp;
public function asListItemRenderer()
{
//TODO: implement function
super();
super.labelField = "title";
super.messageField = "description";
}
/**
* @private
*
* Override this setter to respond to data changes
*/
override public function set data(value:Object):void
{
/*super.data = value;*/
titleDisplay.text = value.title;
msgDisplay.text = trimMessage(value);
link = value.link;
// the data has changed. push these changes down in to the
// subcomponents here
}
/**
* @private
*
* Override this method to create children for your item renderer
*/
override protected function createChildren():void
{
titleDisplay = createDataField(false, false, "bold");
addChild(titleDisplay);
msgDisplay = createDataField(true, true);
addChild(msgDisplay);
readon = new Image();
readon.source="assets/readmore_32px.png";
readon.width=32;
readon.height=32;
addChild(readon);
readon.addEventListener(MouseEvent.CLICK,openlink);
// super.addEventListener(MouseEvent.CLICK,openlink);
// create any additional children for your item renderer here
}
private function openlink(e:Event):void
{
/*if(StageWebView.isSupported==true){
stagewebview=new StageWebView();
stagewebview.viewPort=new Rectangle(0,0,stage.width,stage.height);
stagewebview.stage=this.stage;
stagewebview.loadURL(link);
}else{
warning = new AlertPopUp();
warning.open(this);
}*/
}
/**
* @private
*
* Override this method to change how the item renderer
* sizes itself. For performance reasons, do not call
* super.measure() unless you need to.
*/
override protected function measure():void
{
//super.measure();
// measure all the subcomponents here and set measuredWidth, measuredHeight,
// measuredMinWidth, and measuredMinHeight
measuredHeight = measuredMinHeight = 120;
measuredWidth = measuredMinWidth = unscaledWidth;
}
/**
* @private
*
* Override this method to change how the background is drawn for
* item renderer. For performance reasons, do not call
* super.drawBackground() if you do not need to.
*/
override protected function drawBackground(unscaledWidth:Number,
unscaledHeight:Number):void
{
super.drawBackground(unscaledWidth, unscaledHeight);
// do any drawing for the background of the item renderer here
}
/**
* @private
*
* Override this method to change how the background is drawn for this
* item renderer. For performance reasons, do not call
* super.layoutContents() if you do not need to.
*/
private function createDataField(multiline:Boolean = false, wordWrap:Boolean = false, style:String = "normal"):StyleableTextField
{
var labelField:StyleableTextField = new StyleableTextField();
labelField.styleName = this;
labelField.editable = false;
labelField.selectable = false;
labelField.multiline = multiline;
labelField.wordWrap = wordWrap;
labelField.setStyle("fontSize", 14);
labelField.setStyle("color", 0x333333);
labelField.setStyle("fontWeight", style);
return labelField;
}
override protected function layoutContents(unscaledWidth:Number,
unscaledHeight:Number):void
{
setElementPosition(titleDisplay, 10, 10);
setElementSize(titleDisplay, unscaledWidth, measuredMinHeight);
setElementPosition(msgDisplay, 10, titleDisplay.y + 30);
setElementSize(msgDisplay, unscaledWidth-50, measuredHeight);
setElementPosition(readon,unscaledWidth-32,44);
setElementSize(readon,32,32);
// super.layoutContents(unscaledWidth, unscaledHeight);
// layout all the subcomponents here
}
private function trimMessage(item:Object):String {
return StringUtil.trim(item.description);
}
}
}