无法在flex 4.6中设置List Control的labelField

时间:2012-10-29 16:03:10

标签: sqlite actionscript-3 flex flex4.5 flex4.6

我无法设置我首先保存到SQLite数据库的列表控件标签,然后将其显示为List控件的lebelField,我的代码如下:

城市列表mxml是:

<?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" title="Cities"
    >
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        import model.DataModel;

        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;
        import mx.events.IndexChangedEvent;

        import spark.components.SplitViewNavigator;
        import spark.components.ViewNavigator;
        import spark.transitions.ViewTransitionBase;
        protected function myList_changeHandler():void {
            // Create a reference to the SplitViewNavigator.
            var splitNavigator:SplitViewNavigator = navigator.parentNavigator as SplitViewNavigator;

            // Create a reference to the ViewNavigator for the Detail frame.
            var detailNavigator:ViewNavigator = splitNavigator.getViewNavigatorAt(1) as ViewNavigator;

            detailNavigator.transitionsEnabled = false;

            // Change the view of the Detail frame based on the selected List item.
            detailNavigator.pushView(DisplayContents, list_of_cities.selectedItem);             
        }           
    ]]>
</fx:Script>
<s:VGroup width="100%" height="100%">
    <s:List id="list_of_cities" height="100%" width="100%" change="myList_changeHandler();" 
            dataProvider="{DataModel.getInstance().cityList}">

    </s:List>       
</s:VGroup>

城市价值对象是::

package valueobject
{
[Bindable]
public class CityValueObject
{
    public var id:uint;
    public var nameofcity:String;
}}

和DataModel是::

package model
{
import flash.data.SQLConnection;

import mx.collections.ArrayCollection;

[Bindable]
public class DataModel
{
    public var connection:SQLConnection;
    public var cityList:ArrayCollection = new ArrayCollection();        
    public var logs:String="Application Logs........\n";

    public static var _instance:DataModel;      

    public static function getInstance():DataModel
    {
        if(_instance == null)
        {
            _instance = new DataModel();
        }
        return _instance;
    }
}}

和CityUtilities类是:

package utillities
{
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.ByteArray;

import model.DataModel;

import mx.collections.Sort;
import mx.collections.SortField;

import valueobject.CityValueObject;



public class CityUtils
{
    public static function getAllCities():void
    {
        var contactListStatement:SQLStatement = new SQLStatement();
        contactListStatement.sqlConnection = DataModel.getInstance().connection;
        contactListStatement.text = "SELECT * FROM CITYNAME";
        contactListStatement.execute();
        var result:SQLResult = contactListStatement.getResult();
        if( result.data!=null)
        {
            DataModel.getInstance().cityList.removeAll();

            for(var count:uint=0;count<result.data.length;count++)
            {
                var cityVO:CityValueObject = new CityValueObject();
                cityVO.id = result.data[count].id;
                cityVO.nameofcity = result.data[count].city;                    
                DataModel.getInstance().cityList.addItem(cityVO);
            }
        }
        sortData();     
    }

    public static function sortData():void
    {
        var dataSortField:SortField = new SortField();
        dataSortField.name = "nameofcity";
        dataSortField.numeric = false;

        /* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */
        var numericDataSort:Sort = new Sort();
        numericDataSort.fields = [dataSortField];

        /* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
        DataModel.getInstance().cityList.sort = numericDataSort;
        DataModel.getInstance().cityList.refresh();
    }

    public static function updateLog(newLog:String):void
    {
        DataModel.getInstance().logs += new Date().time+" :-> "+newLog+"\n";
    }
}}

请事先告诉我如何根据SQLite nameofcity列设置labelField

2 个答案:

答案 0 :(得分:1)

您需要显示nameofcity意味着为Spark列表设置LabelField属性,如下所示

<s:VGroup width="100%" height="100%">
<s:List id="list_of_cities" height="100%" width="100%" labelField="nameofcity" change="myList_changeHandler();" 
        dataProvider="{DataModel.getInstance().cityList}">

</s:List>       

如果显示[object CityValueObject],则创建自定义ItemRenderer,然后覆盖数据方法

override public function set data(value:Object):void
{
super.data = value;
var vo:CityValueObject=value as CityValueObject;
lblCityName.text = vo.nameofcity.toString();

}

答案 1 :(得分:0)

所有其他代码都是正确的,但错误的是我放置了

 for(var count:uint=0;count<result.data.length;count++)
        {
            var cityVO:CityValueObject = new CityValueObject();
            cityVO.id = result.data[count].id;
            //cityVO.nameofcity = result.data[count].city; // Here I should Have written like
            cityVO.nameofcity = result.data[count].nameofcity;// this works as I needed        
            DataModel.getInstance().cityList.addItem(cityVO);
        }