I have a problem. I am calling to server side on GWT. The results of
call is an ArrayList (result). Inside onSuccess method another
ArrayList is wrapped with the result. However, if I want to use
ArrayList lista outside of onSuccess method it contains 0 elements,
but inside is like result ArrayList. How can I solve it? I have tried
to use lista as class attribute, static attribute,… but it doesn’t
run.
Well, I have changed my code, but it doesn’t run. I call another method when onSuccess is called…
public void addContainers() throws Exception {
gwtService.obtainAttributesDevice(1, new AsyncCallback<ArrayList<String>>(){
@Override
public void onFailure(Throwable caught) {
System.out.println("ERROR");
Window.alert(caught.getMessage());
}
@Override
public void onSuccess(ArrayList<String> result) {
lista.addAll(0, result);
addProjectContainers();
}
});
private void addProjectContainers() throws Exception {
RootPanel.get("mainContainer").add(new Label("Array lista 2: "+lista.toString()));
RootPanel.get().add(new Label(String.valueOf("Array lista 2: "+lista.size())));
for(int i = 0; i < lista.size(); i++){
RootPanel.get().add(new Label("BOTON "+i));
}
}
If it helps think of the content of onSuccess and onFailure to be threads that run separately from the rest of the code (it’s not really a thread as such, but it’s not run in synchronously with the rest of the code, thats why it’s called “asynchronous”).
It may be help to keep things simpler and avoid massive indenting and also reduces the amount of finals you need to define, if you create a final or member variable referencing
thisin the class doing an asynchCall that the callback can call a method on to handle the response.A pseudo code sample (very simplified):
EDIT About lots of code after the call:
If you need to do lots of “stuff” after the callback, just put it in the onSuccess or the method that you make onSuccess call (such as in my sample).
If you need to make several callbacks in sequence (where reordering them would cause errors) you should first concider making a service method that does combines those several actions (if possible), or you should do another server call inside the onSuccess callback. To simplify this, avoiding very deeply nested code, you could use a custom callback interface to define an anonymous class to use in the first callback that calls the next server call.
I often use this interface to define something to be done after a callback using this interface (or variations of this):