【问题标题】:FLEX - Convert AsyncToken to ArrayCollectionFLEX - 将 AsyncToken 转换为 ArrayCollection
【发布时间】:2014-06-22 03:12:47
【问题描述】:

我是 flex 新手,我似乎无法让它发挥作用。本质上,我有一个选择框,它的可用数据依赖于另一个组合框。

每个 CategoryType 中有多个 TreatmentType。

这是我的代码:

组合框更改;更新选择框:

private function refreshAvailableTreatmentTypes():void {
        // this is the combo box
        fAccomplishment.habitatType = habitatTypeId.selectedIndex != -1 ? habitatTypeId.selectedItem as HabitatType : null; 
        // fAccompRemote is a RemoteObject
        var treatmentList:ArrayCollection = fAccompRemote.getValidTreatments(fAccompForm.accomplishment, fAccomplishment.habitatType);

        if ( fAccompForm.categoryTypes != null ) {
            // All categories are always shown. These are passed to the form on construction. 
            for each ( var currentCat:CategoryType in fAccompForm.categoryTypes ) {
                var catAdded:Boolean = false;
                /* loop through all the treatments in each Category and add them to 
                 * the available list if they meet the criteria */
                for each ( var currentTreat:TreatmentType in currentCat.treatments ) {
                    if (currentTreat.id in treatmentList || treatmentList.length == 0) {
                        if (!catAdded) {
                            // fCatsAndTreats defined as a [Bindable] private var
                            fCatsAndTreats.addItem( currentCat );
                            catAdded = true;
                        } 

                        fCatsAndTreats.addItem( currentTreat );
                    }
                }
            }
        }
    }

服务方式:

@RemotingInclude
public List<TreatmentType> getValidTreatments(Accomplishment accomp, HabitatType selectedHabitatType){
    if ( accomp == null || accomp.getGeometry() == null || accomp.getHabitatType() == null) {
        return new ArrayList<TreatmentType>();
    }

    Geometry accompGeo = accomp.getGeometry();
    List<TreatmentType> optionList = new ArrayList<TreatmentType>();
    String geomName = null;

    if ( accompGeo instanceof Point || accompGeo instanceof MultiPoint ) {
        geomName = "Point";
    } else if ( accompGeo instanceof LineString || accompGeo instanceof MultiLineString) {
        geomName = "Line";
    } else if ( accompGeo instanceof Polygon || accompGeo instanceof MultiPolygon ) {
        geomName = "Polygon";
    }

    Integer habTypeId = null;
    if (selectedHabitatType == null) {
        habTypeId = accomp.getHabitatType().getId();
    } else {
        habTypeId = selectedHabitatType.getId();
    }
    optionList = accomplishmentDao.getValidTreatments(geomName, habTypeId);

    return optionList;
}

TypeError:错误 #1034:类型强制失败:无法将 mx.rpc::AsyncToken@1af48641 转换为 mx.collections.ArrayCollection。

我该怎么做?我找到了THIS,但它似乎对我帮助不大:。任何资源或信息将不胜感激。

【问题讨论】:

    标签: java apache-flex actionscript arraycollection asynctoken


    【解决方案1】:

    对 RemoteObject 的调用是异步的 - fAccompRemote.getValidTreatments 的返回值是一个 AsyncToken,它定义了如何处理结果(何时返回)。

    当远程调用返回时,它将调用结果处理程序或故障处理程序,具体取决于调用是成功还是发生故障。

    您可以通过几种不同的方式为代码设置响应处理程序 - 您可以在调用中添加EventListener,或者在调用返回的 AsyncToken 中设置响应者。

    fAccompRemote.addEventListener(ResultEvent.RESULT, resultHandler);
    fAccompRemote.getValidTreatments(...)
    

    -或-

    var token:AsyncToken = fAccompRemote.getValidTreatments(...);
    token.addResponder(new AsyncResponder(resultHandler, faultHandler));
    

    在任何一种情况下,resultHandler 都会收到一个 ResultEvent 事件,其中包含您从 getValidTreatments 返回的 ArrayCollection

    protected function resultHandler(event:ResultHandler):void
    {
        var results:ArrayCollection = event.result as ArrayCollection;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 2011-01-31
      • 2011-04-01
      相关资源
      最近更新 更多