【问题标题】:Flex 4.6 mobile sqlite specific dataFlex 4.6 移动 sqlite 特定数据
【发布时间】:2012-03-02 18:01:17
【问题描述】:

首先,我是使用 Flex 的新手,但我在在线教程和信息的帮助下拼凑了这个应用程序。基本上,我的应用程序就像一个包含姓名、地址等的目录,但我还有“周”和“日”的附加字段。我想做的是有一个只显示名称的列表,例如第 1 周 - 星期一。下面是我用来帮助你理解我想要做什么的一些代码。感谢您的帮助!

<s:List dataProvider="{AddDoctorDatabase.doctors()}" labelField="name" change="onDoctorSelected(event)"
        left="0" right="0" top="0" bottom="0">
</s:List>


public static function doctors():ArrayCollection
    {
        var doctorList:ArrayCollection = new ArrayCollection();

        var sql:String = "SELECT id, week, day, name, address, city, state, zip, phone FROM doctors";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();
        var sqlResult:SQLResult = stmt.getResult();
        if (sqlResult) {
            var result:Array = sqlResult.data;
            if (result) {
                for (var index:Number = 0; index < result.length; index++) {
                    doctorList.addItem(processRow(result[index]));
                }
            }
        }
        return doctorList;
    }

添加医生

<s:SpinnerListContainer>
        <s:SpinnerList id="weekField" width="100" height="75" labelField="week">
            <s:ArrayList>
                <fx:Object week="Week 1"/>
                <fx:Object week="Week 2"/>
            </s:ArrayList>                        
        </s:SpinnerList>
    </s:SpinnerListContainer>

    <s:Label text="Select a day:"/>
    <s:SpinnerListContainer>
        <s:SpinnerList id="dayField" width="100" height="150" labelField="day">
            <s:ArrayList>
                <fx:Object day="Monday"/>
                <fx:Object day="Tuesday"/>
                <fx:Object day="Wednesday"/>
                <fx:Object day="Thursday"/>
                <fx:Object day="Friday"/>
            </s:ArrayList>                        
        </s:SpinnerList>
    </s:SpinnerListContainer>

protected function onSave():void {
            var newDoctor:AddDoctor = new AddDoctor();
            newDoctor.week = weekField.selectedItem;
            newDoctor.day = dayField.selectedItem;
            newDoctor.name = nameField.text;
            newDoctor.address = addressField.text;
            newDoctor.city = cityField.text;
            newDoctor.state = stateField.text;
            newDoctor.zip = zipField.text;
            newDoctor.phone = phoneField.text;
            AddDoctorDatabase.addDoctor(newDoctor);

【问题讨论】:

  • 我不确定我是否理解你的问题,如果我做你需要的是 s:List 上的 labelFunction 而不是使用 labelField,所以你可以结合你想要显示的字段,该函数应采用 item:Object 参数,该参数将被键入为 dataprovider 的数据元素之一,并且它应返回一个 :String 类似... function myLabelFunction(item:Object):String,在函数中你会做一些事情比如返回 item.weekValue + " - " + item.dayValue
  • sqlite 数据库基本上有 id, week, name, address, city, state, zip, phone。我希望能够填充一个列表,其中显示特定周和特定日期的名称。例如,在星期二显示第 1 周的所有名称。
  • 好的 2 个问题,我想我可以给你一个答案。 1 什么是processRow? 2 您是否有其他输入框可以在其中选择周和日,并且您希望它为您提供仅匹配该周和日的行?
  • 我不是 100% 确定 processRow 是什么,这是我在教程中看到并使用它的东西。至于第二个问题,是的,我有两个 SpinnerLists,允许用户为周字段选择“第 1 周”或“第 2 周”,然后为日字段选择“周一...周五”。我已将代码添加到我的原始帖子中。

标签: apache-flex sqlite mobile


【解决方案1】:

假设这是受Christophe Coenraetsemployee directory sample 启发的,您必须在将新创建的 Doctor 对象添加到 ArrayCollection 中之前,创建一个 Doctor 类并在 processRow 函数中使用检索到的数据设置其公共变量:

<fx:Script>
  <![CDATA[
   //Add this
   [Bindable]
   public var dList:ArrayCollection;



private function searchDoctorsClicked(event:MouseEvent):void
{
    dList = getDoctorForWeekAndDay(weekField.selectedItem, dayField.selectedItem);
}

private function creationComplete_handler(event:Event):void
{
    dList = getAllDoctors();
}

public function getAllDoctors():ArrayCollection
{
    var doctorList:ArrayCollection = new ArrayCollection();

    var sql:String = "SELECT id, week, day, name, address, city, state, zip, phone FROM doctors";
    var stmt:SQLStatement = new SQLStatement();
    stmt.sqlConnection = sqlConnection;
    stmt.text = sql;
    stmt.execute();
    var result:Array = stmt.getResult().data;
    if (result)
    {
       for (var index:Number = 0; index < result.length; index++)
       {
        //doctorList.addItem(processRow(result[index]));
        doctorList.addItem({result[index].week, result[index].day, result[index].name});
       }
       return doctorList;
    }
  else
  { 
     return null;
  }
}
public function processRow(o:Object):Doctor
{
    var d:Doctor = new Doctor();
    d.Name = o.name;
    d.week = o.week;
    d.day  = o.day;

    return d;
}
public class Doctor()
{
    public function Doctor(){}
    public var name:String;
    public var week:String;
    public var day:int;
}

//Call this one when they select a week and day and hit a button, or just
//on change of those options pass through the value/selectedItem from each list
public function getDoctorForWeekAndDay(chosenWeek:String, chosenDay:String):ArrayCollection
{
    var doctorList:ArrayCollection = new ArrayCollection();

    var sql:String = "SELECT id, week, day, name, address, city, state, zip, phone FROM doctors WHERE week='"+chosenWeek+"' AND day='"+chosenDay+"'";
    var stmt:SQLStatement = new SQLStatement();
    stmt.sqlConnection = sqlConnection;
    stmt.text = sql;
    stmt.execute();
    var result:Array = stmt.getResult().data;
    if (result)
    {
       for (var index:Number = 0; index < result.length; index++)
       {
        doctorList.addItem(processRow(result[index]));
       }
       return doctorList;
    }
  else
  { 
     return null;
  }
}
]]>
</fx:Script>

<s:List dataProvider="{dList}" change="onDoctorSelected(event)" left="0" right="0" top="0" bottom="0">
  <s:IconItemRenderer label="{week} - { day }" messageField="name" />
</s:List>

【讨论】:

    【解决方案2】:

    我认为这应该可行
    我不是专家,但我认为这对你有帮助!

    <fx:Script>
      <![CDATA[
       //Add this
       [Bindable]
       public var dList:ArrayCollection;
    
    private function searchDoctorsClicked(event:MouseEvent):void
    {
        dList = getDoctorForWeekAndDay(weekField.selectedItem, dayField.selectedItem);
    }
    
    private function creationComplete_handler(event:Event):void
    {
        dList = getAllDoctors();
    }
    
    public function getAllDoctors():ArrayCollection
    {
        var doctorList:ArrayCollection = new ArrayCollection();
    
        var sql:String = "SELECT id, week, day, name, address, city, state, zip, phone FROM doctors";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();
        var result:Array = stmt.getResult().data;
        if (result)
        {
           for (var index:Number = 0; index < result.length; index++)
           {
            //doctorList.addItem(processRow(result[index]));
            doctorList.addItem({result[index].week, result[index].day, result[index].name});
           }
           return doctorList;
        }
      else
      { 
         return null;
      }
    }
    
    
    //Call this one when they select a week and day and hit a button, or just
    //on change of those options pass through the value/selectedItem from each list
    public function getDoctorForWeekAndDay(chosenWeek:String, chosenDay:String):ArrayCollection
    {
        var doctorList:ArrayCollection = new ArrayCollection();
    
        var sql:String = "SELECT id, week, day, name, address, city, state, zip, phone FROM doctors WHERE week='"+chosenWeek+"' AND day='"+chosenDay+"'";
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = sql;
        stmt.execute();
        var result:Array = stmt.getResult().data;
        if (result)
        {
           for (var index:Number = 0; index < result.length; index++)
           {
            //doctorList.addItem(processRow(result[index]));
            doctorList.addItem({result[index].week, result[index].day, result[index].name});
           }
           return doctorList;
        }
      else
      { 
         return null;
      }
    }
    ]]>
    </fx:Script>
    
    <s:List dataProvider="{dList}" change="onDoctorSelected(event)" left="0" right="0" top="0" bottom="0">
      <s:IconItemRenderer label="{week} - { day }" messageField="name" />
    </s:List>
    

    【讨论】:

    • 我认为这可行,但我收到错误 1137: Incorrect number of arguments, expected not more than 1 for the following line doctorList.addItem(result[index].week, result[index ].day, result[index].name);
    • 当我切换到列表视图的设计模式时,我也遇到了一个错误,我以前从未见过这个,但我看不到任何设计。它只是说“如果该属性也显式设置为标签或属性,则无法为默认属性 dataprovider 指定值。”
    • 你必须在代码的其他部分初始化 dList 变量,尝试在另一个函数中执行这个 'dList=doctors()',而不是在 dataProvider (dataProvider="{dList}")
    • 而且,processRow 函数用于 Flex 示例,对吗?这个函数把数组处理成一个Object,例子中用到了object,我没有用这个函数,但是如果你想看代码修改它=)
    • @jclandero23 我想你已经猜对了一半我会试着编辑你上面的帖子,让它更符合现实,但我认为原帖应该更一般地解释什么每个班级都涉及到问题,因为班级的命名给我带来了很多困惑。在您的帖子中,您没有根据您的评论进行更新,所以我会修复这些东西,而且您在 addItem 中缺少 {},否则我认为只需要运行更多 SQL 即可根据所选的周/日获取它们,但没有图表/线框,我不知道到底需要什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多