0

我无法设置列表控件的标签,我首先将其保存到 SQLite 数据库,然后将其显示为列表控件的 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 提前谢谢

4

2 回答 2

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();

}

于 2012-10-30T08:12:31.910 回答
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);
        }
于 2012-10-31T19:33:39.100 回答