Flex 在列表项的视图之间传递数据
Posted
技术标签:
【中文标题】Flex 在列表项的视图之间传递数据【英文标题】:Flex pass data between views from a list item 【发布时间】:2012-10-26 01:30:17 【问题描述】:大家好,首先感谢您的关注和支持。我是 flex 世界的初学者……我想做的事情可能太简单了,但我想不通。
我正在练习一个简单的移动应用程序,它使用 phpmyadmin 中的数据库并连接到我的移动项目。我想要做的是:当我单击任何列表项时,会弹出下一个视图,显示数据库表中所有文本输入中所选项目的值。我知道如何在我正在实现列表的视图中显示它,但我收到错误消息,因为我在其他视图中没有(也不想)列表项:
protected function list_changeHandler(event:IndexChangeEvent):void
titleTextInput.text = list.selectedItem.title;
dateIntro.selectedDate = list.selectedItem.date;
photoTextInput.text = list.selectedItem.photo;
descriptionTextInput.text = list.selectedItem.description;
但是我需要做什么才能在我的其他视图中获得相同的结果?
该项目有 2 个视图,
这是第一个视图
<?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:valueObjects="valueObjects.*"
xmlns:detallesservice="services.detallesservice.*" add="home(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import spark.events.IndexChangeEvent;
protected function button_clickHandler(event:MouseEvent):void
var detalles2:Detalles = new Detalles();
detalles2.title = titleTextInput.text;
detalles2.date = dateIntro.selectedDate;
detalles2.photo = photoTextInput.text;
detalles2.description = descriptionTextInput.text;
detallesService.createDetalles(detalles2);
detallesService.getAllDetalles();
protected function list_creationCompleteHandler(event:FlexEvent):void
getAllDetallesResult2.token = detallesService.getAllDetalles();
protected function button2_clickHandler(event:MouseEvent):void
detallesService.deleteDetalles(list.selectedItem.title);
protected function list_changeHandler(event:IndexChangeEvent):void
titleTextInput.text = list.selectedItem.title;
dateIntro.selectedDate = list.selectedItem.date;
photoTextInput.text = list.selectedItem.photo;
descriptionTextInput.text = list.selectedItem.description;
navigator.pushView(asyouwishView, list.selectedItem);
]]>
</fx:Script>
<fx:Declarations>
<valueObjects:Detalles id="detalles"/>
<detallesservice:DetallesService id="detallesService"/>
<s:CallResponder id="createDetallesResult"/>
<s:CallResponder id="getAllDetallesResult2"/>
<s:CallResponder id="deleteDetallesResult"/>
</fx:Declarations>
<s:List id="list" x="1100" y="142"
creationComplete="list_creationCompleteHandler(event)" labelField="title" change="list_changeHandler(event)">
<s:AsyncListView list="getAllDetallesResult2.lastResult"/>
</s:List>
<s:Form defaultButton="button">
<s:FormItem label="Title">
<s:TextInput id="titleTextInput" text="detalles.title"/>
</s:FormItem>
<s:FormItem label="Date">
<s:DateSpinner id="dateIntro" displayMode="date" selectedDate="detalles.date"/>
</s:FormItem>
<s:FormItem label="Photo">
<s:TextInput id="photoTextInput" text="detalles.photo"/>
</s:FormItem>
<s:FormItem label="Description">
<s:TextInput id="descriptionTextInput" text="detalles.description"/>
</s:FormItem>
<s:Button id="button" label="CreateDetalles" click="button_clickHandler(event)"/>
</s:Form>
<s:Form x="18" y="950">
<s:FormItem label="CreateDetalles">
<s:TextInput id="createDetallesTextInput"
text="createDetallesResult.lastResult as String"/>
</s:FormItem>
</s:Form>
<s:Button id="myDelete" x="1100" y="558" label="Delete" click="button2_clickHandler(event)"/>
这是第二种观点:
<fx:Script>
<![CDATA[
import spark.events.ViewNavigatorEvent;
protected function button_clickHandler(event:MouseEvent):void
// Please uncomment the below line if Data Management is enabled for Detalles and updateDetalles is used as the create function.
// var detalles:Detalles = new Detalles();
detalles.title = titleTextInput.text;
detalles.date = dateIntro.selectedDate;
detalles.photo = photoTextInput.text;
detalles.description = descriptionTextInput.text;
updateDetallesResult.token = detallesService.updateDetalles(detalles);
]]>
</fx:Script>
<fx:Declarations>
<valueObjects:Detalles id="detalles"/>
<detallesservice:DetallesService id="detallesService"/>
<s:CallResponder id="updateDetallesResult"/>
</fx:Declarations>
<s:actionContent>
<s:Button label="BACK" click="navigator.popView();"/>
</s:actionContent>
<s:Form x="70" y="68" defaultButton="button">
<s:FormItem label="Title">
<s:TextInput id="titleTextInput" text="@detalles.title"/>
</s:FormItem>
<s:FormItem label="Date">
<s:DateSpinner id="dateIntro" displayMode="date" selectedDate="@detalles.date"/>
</s:FormItem>
<s:FormItem label="Photo">
<s:TextInput id="photoTextInput" text="@detalles.photo" />
</s:FormItem>
<s:FormItem label="Description">
<s:TextInput id="descriptionTextInput" text="@detalles.description"/>
</s:FormItem>
<s:Button id="button" label="UpdateDetalles" click="button_clickHandler(event)"/>
</s:Form>
【问题讨论】:
【参考方案1】:学习和使用框架是必要的。我推荐Swizframework。
Swiz 中的数据绑定和事件轻而易举。你会喜欢的。
【讨论】:
感谢您的建议 =) 我试试看,您知道学习如何使用它的更好方法吗? 用它来做点什么,这是学习新东西的最佳方式。以上是关于Flex 在列表项的视图之间传递数据的主要内容,如果未能解决你的问题,请参考以下文章
在没有 Segues 的 ViewController 之间传递数据