質問

At first I want to say that I'm not sure if it's the correct way to implement my callback into the Activity class. My goal is to fetch a list of users from a db and set them into my view.

public class UserListActivity extends AbstractActivity implements UserListView.Presenter<GWTUser> {

    private List<GWTUser> users;

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        view.setPresenter(this);
        fetchUsers();
        if (getUsers() == null) {
            setUsers(new ArrayList<GWTUser>());
        }

        view.setUserList(getUsers());
        panel.setWidget(view.asWidget());

    }

    @Override
    public void fetchUsers() {

        Defaults.setServiceRoot(GWT.getHostPageBaseURL());
        UserAdminRestService userService = GWT.create(UserAdminRestService.class);
        ((RestServiceProxy)userService).setResource(new Resource("/user"));

        userService.getUsers(new MethodCallback<List<GWTUser>>() {



            @Override
            public void onFailure(Method method, Throwable exception) {
                // TODO getUsers error handling
                System.out.println(exception.getMessage().toString());

            }

            @Override
            public void onSuccess(Method method, List<GWTUser> response) {
                setUsers(response);
            }

        });

    }


    private void setUsers(List<GWTUser> users) {
        this.users = users;
    }

    private List<GWTUser> getUsers() {
        return this.users;
    }

The Rest call basically works because I can see the response in my Development Mode (Eclipse) but when I try to debug this procedure the MethodCallback got overstepped and my users list is always empty. I'm not sure where the problem is, seems like it has something to do with when to call the fetchUsers method. Or it run-time or compile-time depending, not sure. Thanks for any help.

役に立ちましたか?

解決 2

fixed the problem by setting the the arraylist into the view afterthe response meaning

@Override
public void start(AcceptsOneWidget panel, EventBus eventBus) {
    view.setPresenter(this);
    fetchUsers();
    panel.setWidget(view.asWidget());

}

@Override
        public void onSuccess(Method method, List<GWTUser> response) {
            view.setUserList(response);
        }

So the actual user list is only set after the response.

他のヒント

The following code

if (getUsers() == null) {
        setUsers(new ArrayList<GWTUser>());
    }

is executed immediately - before your service had a chance to receive a response. You need to move this code inside the onSuccess method in your callback:

if (response == null) {
        setUsers(new ArrayList<GWTUser>());
    } else {
        setUsets(response);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top