如何允许单个项/子编辑器GWT编辑器从包含它的ListEditor中删除自身?e、 g.点击移除按钮

如何允许单个项/子编辑器GWT编辑器从包含它的ListEditor中删除自身?e、 g.点击移除按钮,gwt,indexoutofboundsexception,gwt-editors,Gwt,Indexoutofboundsexception,Gwt Editors,在尝试使用GWT的ListEditor系统时,我找不到一个工作示例,其中列表中每个项目的UI都有一个delete/remove按钮 我找到的示例都与[1]类似,并且都有一个EditorSource.create()实现创建每个项编辑器,并似乎连接了一个处理程序,通过listEditor.getList().remove(index)从基础列表中删除该项 但是,删除处理程序的匿名实现在创建子编辑器时关闭索引的值,这会导致IndexOutOfBoundExceptions或删除错误的项,因为每次删除

在尝试使用GWT的
ListEditor
系统时,我找不到一个工作示例,其中列表中每个项目的UI都有一个delete/remove按钮

我找到的示例都与[1]类似,并且都有一个
EditorSource.create()
实现创建每个项
编辑器
,并似乎连接了一个处理程序,通过
listEditor.getList().remove(index)
从基础列表中删除该项

但是,删除处理程序的匿名实现在创建子编辑器时关闭索引的值,这会导致
IndexOutOfBoundExceptions
或删除错误的项,因为每次删除都会更改后面所有项的索引

我把我的头发拔了一段时间,试图看看我在阻止这种情况发生的例子中遗漏了什么,但从我所能说的,他们确实都有这个问题,所以尽管修复相当简单,我仍然会在这里发布它,这样人们至少可以找到一个正确移除物品的例子


[1] 我认为我找到的所有示例都是从我链接的示例中派生出来的,尽管其中一个示例在remove()中有更多的逻辑,并且可能已经采取了一些措施来避免问题,例如以某种方式更正列表顺序,我还没有深入研究该项目中的其他代码。

以下是一个最小的
ListEditor
示例,它纠正了在其他示例中发现的问题

public abstract class FooEditor extends Composite implements Editor<Foo> {

    Widget root; // Instantiated explicitly or through uibinder

    // Implemented as one of uibinder+fields, fields, methods, or LeafValueEditor.set/getValue()

    public FooEditor() { 
        initWidget(root); 
    }

    // Used for brevity, could be any triggering mechanism, click handler, event handler, etc.
    abstract void onDeleteClicked(); 
}

public class FooListEditor extends Composite implements IsEditor<ListEditor<Foo, FooEditor>> {

    private class FooEditorSource extends EditorSource<FooEditor> {

        @Override 
        public FooEditor create(int index) {

            FooEditor subEditor = new FooEditor()
            {
                @Override
                public void onDeleteClicked()
                {
                    // =======================================================
                    //
                    // This fixes the problem present in other examples
                    // by determining the current index at the time of removal
                    //
                    // =======================================================
                    int currentIndex = listEditor.getEditors().indexOf(this);
                    listEditor.getList().remove(currentIndex);    
                }
            };

            setIndex(subEditor, index);

            return subEditor;
        }

        @Override 
        public void dispose(FooEditor subEditor) { 
            subEditor.removeFromParent(); 
        }

        @Override 
        public void setIndex(FooEditor subEditor, int index) {
            listPanel.insert(subEditor, index);
        }
    }

    FlowPanel listPanel; // Instantiated explicitly or through uibinder

    ListEditor<Foo, FooEditor> listEditor = ListEditor.of(new FooEditorSource());

    public FooListEditor() {
        initWidget(listPanel);
    }

    @Override 
    public ListEditor<Foo, FooEditor> asEditor() { 
        return listEditor; 
    }
}
公共抽象类FooEditor扩展了复合实现编辑器{
Widget root;//显式或通过uibinder实例化
//作为uibinder+字段、字段、方法或LeafValueEditor.set/getValue()之一实现
公共FooEditor(){
initWidget(根);
}
//为简洁起见,可以是任何触发机制、单击处理程序、事件处理程序等。
抽象void();
}
公共类傻瓜编辑器扩展复合实现IsEditor{
私有类FooEditorSource扩展了EditorSource{
@凌驾
公共编辑器创建(int索引){
FooEditor子编辑器=新建FooEditor()
{
@凌驾
已单击的公共void()
{
// =======================================================
//
//这解决了其他示例中存在的问题
//通过在移除时确定当前索引
//
// =======================================================
int currentIndex=listEditor.getEditors().indexOf(this);
listEditor.getList().remove(currentIndex);
}
};
setIndex(子编辑器,索引);
返回子编辑器;
}
@凌驾
public void dispose(FooEditor子编辑器){
subEditor.removeFromParent();
}
@凌驾
公共void集合索引(FooEditor子编辑器,int索引){
插入(子编辑器,索引);
}
}
FlowPanel listPanel;//显式或通过uibinder实例化
ListEditor ListEditor=ListEditor.of(new FooEditorSource());
公共编辑程序(){
initWidget(列表面板);
}
@凌驾
public ListEditor asEditor(){
返回监听器;
}
}

非常感谢您添加此功能。对社区绝对有用:)