<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="406" height="340">
<mx:Panel x="10" y="10" width="379" height="315" layout="absolute" title="Listサンプル">
<mx:List id="userList" x="10" y="10" width="279" height="148" dataProvider="{userData}" change="onChange()" labelFunction="{modifyDisplay}"/>
<mx:Button id="delBtn" x="297" y="73" label="削除" click="onDelete()"/>
<mx:Label x="10" y="179" text="ユーザID"/>
<mx:TextInput id="userId" x="79" y="175" width="91"/>
<mx:Label x="10" y="210" text="氏"/>
<mx:TextInput id="lastName" x="79" y="208" width="91"/>
<mx:Label x="178" y="210" text="名"/>
<mx:TextInput id="firstName" x="198" y="208" width="91"/>
<mx:Button id="updateBtn" x="212" y="243" label="登録/更新" click="onUpdate()"/>
</mx:Panel>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private var userData:Array = new Array(
{userId:"taro",lastName:"山田",firstName:"太郎"},
{userId:"jiro",lastName:"田中",firstName:"二郎"},
{userId:"saburo",lastName:"佐藤",firstName:"三郎"}
)
private function modifyDisplay(item:Object):String
{
return item.userId + " " + item.lastName + item.firstName;
}
private function onDelete():void
{
if(userList.selectedIndex != -1)
{
var arrayC:ArrayCollection = ArrayCollection(userList.dataProvider);
arrayC.removeItemAt(userList.selectedIndex);
userId.text = null;
lastName.text = null;
firstName.text = null;
}
}
private function onChange():void
{
var item:Object = userList.selectedItem;
userId.text = item.userId;
lastName.text = item.lastName;
firstName.text = item.firstName;
}
private function onUpdate():void
{
var arrayC:ArrayCollection = ArrayCollection(userList.dataProvider);
for(var i:String in arrayC)
{
if(arrayC[i].userId == userId.text)
{
arrayC[i].lastName = lastName.text;
arrayC[i].firstName = firstName.text;
arrayC.itemUpdated(arrayC[i]);
return;
}
}
var user:Object = new Object();
user.userId = userId.text;
user.lastName = lastName.text;
user.firstName = firstName.text;
arrayC.addItem(user);
}
]]>
</mx:Script>
<mx:Style>
List { font-size:14; border-color:gray;}
Label { font-size:14; }
TextInput { font-size:14; }
</mx:Style>
</mx:Application>