I have this code:

    searchButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {

            statusLabel.setText("Searching...");

            final String query = searchField.getText();
            RootPanel.get("flickr").clear();
            AsyncCallback<Flickr> ac=new AsyncCallback<Flickr>(){

                @Override
                public void onFailure(Throwable caught) {

                }

                @Override
                public void onSuccess(Flickr result) {

                    for(Photo p:result.getPhotos().getPhoto())
                    {
                        flck.add(p);
                    }
                    statusLabel.setText("");
                }
            };

            mashupService.getFlickrPhotos(query, ac);

            if(!flck.isEmpty())
            {
                for(int i=0;i<flck.size();i++)
                {
                    RootPanel.get("flickr").add(new HTML("<img  src='http://farm"+flck.get(i).getFarm()+".staticflickr.com/"+flck.get(i).getServer()+"/"+flck.get(i).getId()+"_"+flck.get(i).getSecret()+".jpg'/><br/>"));

                }
            }

            }
    });

I want execute first onSuccess (have flick.add)... but it executes after of if(!flck.isEmpty)... and I need have flck with data but I can't...

When I press secont time the same button, flck have data of first onClick...

Thanks in advance

有帮助吗?

解决方案

Move the code inside the onSuccess() method that is depended on the result of AsyncCallback.


It's clear from the name that AsyncCallback is just like a AJAX request that talks to server asynchronously means the execution of code is not sequential.

Just move if(!flck.isEmpty)... inside onSuccess() method.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top