GWT中的AsyncCallback问题

时间:2012-07-18 22:14:58

标签: java gwt asynccallback

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

但是当我调用此方法时,它会在Callback组成列表之前返回列表。

`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;
    }`

我该怎么办?

2 个答案:

答案 0 :(得分: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

中找到有关异步远程服务器通信的优秀教程

答案 1 :(得分:0)

我解决了发送到方法的问题,这些项目从DB thx加载到所有。