0

嗨,我是 gwt 编程的初学者,我已经解决了这个 AsyncCallback 的问题,我的代码是这样的:

但是当我调用这个方法时,它会在回调可以组成列表之前返回列表。

`public static ArrayList<Appointment> build(Usuario user,AppointmentStyle[] styles) {
        final ArrayList<Appointment> list = new ArrayList<Appointment>();
        callback = new AsyncCallback<List<Compromisso>>() {

            @Override
            public void onFailure(Throwable caught) {
                MessageBox.alert("Erro", "Contate o administrador", null);

            }

            @Override
            public void onSuccess(List<Compromisso> result) {
                compromissos = result;
            }

        };
        getServico().listCompromisso(user, callback);


        for (int i = 0; i < compromissos.size(); i++) {
            Date now = new Date();
            now.setDate(compromissos.get(i).getData().getDate());

            Date start = (Date) now.clone();
            int hour = compromissos.get(i).getHorai().getHours();
            int min = compromissos.get(i).getHorai().getMinutes();
            start.setHours(hour);
            start.setMinutes(min);

            Date end = (Date) now.clone();
            end.setHours(compromissos.get(i).getHoraf().getHours());
            end.setMinutes(compromissos.get(i).getHoraf().getMinutes());

            Appointment appt = new Appointment();
            appt.setStart(compromissos.get(i).getHorai());
            appt.setEnd(compromissos.get(i).getHoraf());

            if (compromissos.get(i).getPrivacidade().getCodigo() == "1") {
                appt.setTitle(compromissos.get(i).getTitulo());
                appt.setDescription(compromissos.get(i).getNota());
            } else if (compromissos.get(i).getPrivacidade().getCodigo() == "2") {
                appt.setTitle(compromissos.get(i).getTitulo());
                appt.setDescription("Dados n&atilde;o disponiveis.");
            } else {

                appt.setTitle("Ocupado");
                appt.setDescription("Dados n&atilde;o disponiveis.");
                appt.setStyle(AppointmentStyle.GREY);
            }
            if (compromissos.get(i).getPrioridade().getCodigo() == "1") {
                appt.setStyle(AppointmentStyle.RED);
            } else if (compromissos.get(i).getPrioridade().getCodigo() == "2") {
                appt.setStyle(AppointmentStyle.GREEN);
            } else if (compromissos.get(i).getPrioridade().getCodigo() == "3") {
                appt.setStyle(AppointmentStyle.GREEN);
            }
            list.add(appt);
        }

        return list;
    }`

我该怎么办 ?

4

2 回答 2

0

您应该异步处理您的请求。问题是getServico().listCompromisso(user, callback);不等待服务器响应。

在 onSuccess 中调用您的列表处理并更新 ui:

public static void updateModel (Usuario user,AppointmentStyle[] styles) { 
    final ArrayList list = new ArrayList();
    callback = new AsyncCallback>() {

        @Override
        public void onFailure(Throwable caught) {
            MessageBox.alert("Erro", "Contate o administrador", null);

        }

        @Override
        public void onSuccess(List<Compromisso> result) {
            compromissos = result;
            processResult();
            updateUi(); //TODO update your App(-UI)
        }

    };
    getServico().listCompromisso(user, callback);
 }

 /* I don't read all this stuff */
 processResult(){
     for (int i = 0; i < compromissos.size(); i++) {
        Date now = new Date();
        now.setDate(compromissos.get(i).getData().getDate());

        Date start = (Date) now.clone();
        int hour = compromissos.get(i).getHorai().getHours();
        int min = compromissos.get(i).getHorai().getMinutes();
        start.setHours(hour);
        start.setMinutes(min);

        Date end = (Date) now.clone();
        end.setHours(compromissos.get(i).getHoraf().getHours());
        end.setMinutes(compromissos.get(i).getHoraf().getMinutes());

        Appointment appt = new Appointment();
        appt.setStart(compromissos.get(i).getHorai());
        appt.setEnd(compromissos.get(i).getHoraf());

        if (compromissos.get(i).getPrivacidade().getCodigo() == "1") {
            appt.setTitle(compromissos.get(i).getTitulo());
            appt.setDescription(compromissos.get(i).getNota());
        } else if (compromissos.get(i).getPrivacidade().getCodigo() == "2") {
            appt.setTitle(compromissos.get(i).getTitulo());
            appt.setDescription("Dados n&atilde;o disponiveis.");
        } else {

            appt.setTitle("Ocupado");
            appt.setDescription("Dados n&atilde;o disponiveis.");
            appt.setStyle(AppointmentStyle.GREY);
        }
        if (compromissos.get(i).getPrioridade().getCodigo() == "1") {
            appt.setStyle(AppointmentStyle.RED);
        } else if (compromissos.get(i).getPrioridade().getCodigo() == "2") {
            appt.setStyle(AppointmentStyle.GREEN);
        } else if (compromissos.get(i).getPrioridade().getCodigo() == "3") {
            appt.setStyle(AppointmentStyle.GREEN);
        }
        list.add(appt);
    }
}

编辑:

您可以在GWT-Documentation中找到关于异步远程服务器通信的好教程

于 2012-07-18T23:25:32.333 回答
0

我解决了将项目区域从 DB thx 加载到所有方法的问题。

于 2012-07-26T13:45:36.260 回答