使用ListDataProvider支持的CellList不会在列表更改时重新绘制

时间:2011-02-03 09:27:59

标签: gwt

我正在开发一个使用GWT 2.1和mvp4g的项目。在一个视图中,我正在使用 一个由ListDataProvider支持的CellList。如果我将带有数据的List传递给构造函数 在实例化ListDataProvider时,CellList显示此数据。 问题是,在此之后,CellList永远不会被重绘 每当我更改ListDataProvider中的列表时。我不知道我是什么 做错了或错过了什么。

以下是代码:

UIBinder xml文件:

<g:DockLayoutPanel unit="PX">
    <g:west size="300">
        <g:VerticalPanel styleName='{style.leftPanel}' spacing="8">
            <g:Label>Expositores</g:Label>
                <g:ScrollPanel addStyleNames='{style.exhibitorList}' width="250px" height="600px">
                    <c:CellList ui:field="exhibitorList" />
                </g:ScrollPanel>
            <g:Button ui:field="editExhibitorButton" addStyleNames='{style.button}'>Editar</g:Button>
        </g:VerticalPanel>
    </g:west>

    ...

View类:

public class ExhibitorsAdminView extends Composite implements 
                ExhibitorsAdminPresenter.IExhibitorsAdminView { 
        interface Binder extends UiBinder<Widget, ExhibitorsAdminView> {} 
        private static final Binder binder = GWT.create( Binder.class ); 
        private static class ExhibitorCell extends AbstractCell<Exhibitor> { 
                @Override 
                public void render(Cell.Context context, Exhibitor exhibitor, 
                                SafeHtmlBuilder sb) { 
                        if (exhibitor != null) { 
                                sb.appendEscaped(exhibitor.getName()); 
                        } 
                } 
        } 
        private ListDataProvider<Exhibitor> exhibitorsDataProvider; 
        private SingleSelectionModel<Exhibitor> exhibitorsSelectionModel; 
        @UiField( provided = true ) 
        CellList<Exhibitor> exhibitorList; 
        @UiField 
        Button editExhibitorButton; 
//      @UiField(provided = true) 
//      CellTable<Object> moduleList = new CellTable<Object>(); 
        public ExhibitorsAdminView() { 
                exhibitorsSelectionModel = new 
SingleSelectionModel<Exhibitor>(Exhibitor.KEY_PROVIDER); 
                exhibitorList = new CellList<Exhibitor>(new ExhibitorCell(), 
Exhibitor.KEY_PROVIDER); 
                exhibitorList.setSelectionModel(exhibitorsSelectionModel); 
                exhibitorsDataProvider = new 
ListDataProvider<Exhibitor>(getExhibitors()); 
                exhibitorsDataProvider.addDataDisplay(exhibitorList); 
                exhibitorList.setPageSize(exhibitorsDataProvider.getList().size()); 
                initWidget( binder.createAndBindUi( this ) ); 
        } 
        public SingleSelectionModel<Exhibitor> getExhibitorsSelectionModel() 
{ 
                return exhibitorsSelectionModel; 
        } 
        public ListDataProvider<Exhibitor> getExhibitorsDataProvider() { 
                return exhibitorsDataProvider; 
        } 
        private List<Exhibitor> getExhibitors() { 
                List<Exhibitor> exhibitors = new ArrayList<Exhibitor>(); 
                for (int i = 0; i < 10; i++) { 
                        exhibitors.add(new Exhibitor(i, "aaaaaaaaaaaaaaa")); 
                } 
                return exhibitors; 
        } 
        public HasClickHandlers getEditExhibitorButton() { 
                return editExhibitorButton; 
        } 
}

演示者课程:

@Presenter(view = ExhibitorsAdminView.class) 
public class ExhibitorsAdminPresenter extends 
                BasePresenter<ExhibitorsAdminPresenter.IExhibitorsAdminView, 
ExhibitorsEventBus> { 
        public interface IExhibitorsAdminView { 
                SingleSelectionModel<Exhibitor> getExhibitorsSelectionModel(); 
                ListDataProvider<Exhibitor> getExhibitorsDataProvider(); 
                HasClickHandlers getEditExhibitorButton(); 
        } 
        private DispatchAsync dispatch = null; 
        @Inject 
        public ExhibitorsAdminPresenter(final DispatchAsync dispatch) { 
                this.dispatch = dispatch; 
        } 
        @Override 
        public void bind() { 
                getView().getExhibitorsSelectionModel().addSelectionChangeHandler( 
                                new SelectionChangeEvent.Handler() { 
                                        public void onSelectionChange(SelectionChangeEvent event) { 
                                                Exhibitor selected = 
getView().getExhibitorsSelectionModel().getSelectedObject(); 
                                                if (selected != null) { 
                                                        Window.alert("You selected: " + selected.getName()); 
                                                } 
                                        } 
                                }); 
                getView().getEditExhibitorButton().addClickHandler( 
                                new ClickHandler() { 
                                        public void onClick(ClickEvent event) { 
                                        } 
                                }); 
        } 
        public void onGoToExhibitorsAdmin() { 
        } 
        public void onLoadExhibitors() { 
                dispatch.execute(new GetExhibitors(), new 
AsyncCallback<GetExhibitorsResult>() { 
                        public void onSuccess(GetExhibitorsResult result) { 
                                getView().getExhibitorsDataProvider().setList( 
                                                result.getExhibitors()); 
                                getView().getExhibitorsDataProvider().refresh(); 
                        } 
                        public void onFailure(Throwable caught) { 
                                GWT.log("error executing command ", caught); 
                        } 
                }); 
        } 
}

感谢。

3 个答案:

答案 0 :(得分:1)

我解决了。对不起,这是与mvp4g有关的问题。我做错了导致不得不放置CellList的视图的不同实例。我在ListDataProvider列表上进行的更新操作正在未显示的视图实例上完成。

答案 1 :(得分:0)

您必须首先获取列表,例如provider.getList().add(...),才能操纵列表。有关最小示例,请参阅How to add or remove a single element from/to CellList?

答案 2 :(得分:0)

在使用基础列表进行所有操作后,只需调用exhibitorsDataProvider.refresh()