here's my code so far (this is using GWT):
private ArrayList<tObjects> getSuggestions(String query)
{
// Clear previous suggestions
Window.alert("Clearing arraylist");
arrayList.clear();
query = query.toUpperCase().replace(" ", "");
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "xmlfile.php?query="+query);
rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
try
{
rb.sendRequest(null, new RequestCallback()
{
@Override
public void onResponseReceived(com.google.gwt.http.client.Request request, com.google.gwt.http.client.Response response)
{
Window.alert(response.getText());
// Do a lot of data processing here.
Window.alert("Adding to arraylist");
addToArrayList(data);
}
}
@Override
public void onError(com.google.gwt.http.client.Request request, Throwable exception)
{
// TODO Auto-generated method stub
}
});
}
catch(RequestException e)
{
Window.alert(e.toString());
}
Window.alert("Returning arraylist. "+arrayList.toString());
return arrayList;
}
I receive the response correctly, but the method returns the (empty) arrayList before it is added. If i remove the arrayList.clear(), on the next ajax call I see the result of the previous call. When I look at the alerts, the fire in this order:
1) "Clearing arrayList."
2) "Returning arrayList."
3) Alert with correct response from ajax
4) "Adding to arrayList"
It seems like the method is not waiting for the ajax to complete before returning and finishing the method. How can I make it wait for the ajax & population of the arrayList before I get to the return statement?
Thanks!