Gwt ListDataProvider.getList().remove不';不要使用密钥提供者
调用ListDataProvider.getList().remove(…)不使用传入构造函数的KeyProvider。这是一个GWT bug还是应该像这样工作? 我在下面附上了相关的单元测试 谢谢,Gwt ListDataProvider.getList().remove不';不要使用密钥提供者,gwt,celltable,gwt-celltable,Gwt,Celltable,Gwt Celltable,调用ListDataProvider.getList().remove(…)不使用传入构造函数的KeyProvider。这是一个GWT bug还是应该像这样工作? 我在下面附上了相关的单元测试 谢谢, TestVO ri1 = new TestVO(); ri1.setId(null); ri1.setName("msg1"); TestVO ri2 = new TestVO(); ri2.setId(null); ri2.setName("msg
TestVO ri1 = new TestVO();
ri1.setId(null);
ri1.setName("msg1");
TestVO ri2 = new TestVO();
ri2.setId(null);
ri2.setName("msg2");
TestVO ri3 = new TestVO();
ri3.setId(null);
ri3.setName("msg2");
ListDataProvider<TestVO> ldp = new ListDataProvider<>(new ProvidesKey<TestVO>() {
@Override
public Object getKey(TestVO pItem) {
System.out.println("this never gets invoked");
return pItem.getMessageType();
}
});
ldp.setList(Lists.newArrayList(ri1, ri2));
ldp.getList().remove(ri3);
//this currently fails, actual size is 1 as it removes ri2
assertEquals(2, ldp.getList().size());
testvori1=newtestvo();
ri1.setId(空);
ri1.设置名称(“msg1”);
TestVO ri2=新的TestVO();
ri2.setId(空);
ri2.集合名(“msg2”);
TestVO ri3=新的TestVO();
ri3.setId(空);
ri3.设置名称(“msg2”);
ListDataProvider ldp=新的ListDataProvider(新的ProviderKey(){
@凌驾
公共对象getKey(testvopitem){
System.out.println(“这永远不会被调用”);
返回pItem.getMessageType();
}
});
ldp.setList(Lists.newArrayList(ri1,ri2));
ldp.getList().remove(ri3);
//此操作当前失败,实际大小为1,因为它删除了ri2
assertEquals(2,ldp.getList().size());
不,这不是一个bug,也不使用您为查找该列表中的元素而提供的getKey
实现,因为该方法用于其他操作。请容忍我
ListDataProvider
使用实现List
接口的ListWrapper
,并且ListWrapper
由保存对象的普通List
支持。ListWrapper
为您正在使用的方法(添加、设置、删除等)提供了实现,这些方法操作您传递的Lists.newArrayList(ri1、ri2)
此操作通过调用列表
接口的标准操作来完成。因此,用来确定应该删除哪个对象的调用是indexOf
,比如说ArrayList
(这就是您要传递的内容,这就是正在使用的内容)使用的(正如您所期望的)TestVO
的equals
方法,如果您没有重写该方法,则可以在对象
类中找到该方法
您提供的getKey
方法用于标识行列表中的行(在您使用的显示器中,如DataGrid
)。这可以在AbstractDataProvider
中找到。这个调用您的方法的方法被其他方法调用(请参见我在那里做的:P),以确定需要执行某些操作的行。例如,DataGrid
中的行的更新。确定更新了哪一行的方法是您提供的方法。因此,如果您的实现没有提供唯一的键,那么您将指示对具有相同键的所有行进行更新。所以要小心
/**
* Get the key for a list item. The default implementation returns the item
* itself.
*
* @param item the list item
* @return the key that represents the item
*/
public Object getKey(T item) {
return keyProvider == null ? item : keyProvider.getKey(item);
}
在ArrayList
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
public int indexOf(对象o){
如果(o==null){
对于(int i=0;i
这是GWT的实现
/**
* Construct a new {@link ListWrapper} that delegates flush calls to the
* specified delegate.
*
* @param list the list to wrap
* @param delegate the delegate
* @param offset the offset of this list
*/
private ListWrapper(List<T> list, ListWrapper delegate, int offset) {
this.list = list;
this.delegate = delegate;
this.offset = offset;
}
@Override
public void add(int index, T element) {
try {
list.add(index, element);
minModified = Math.min(minModified, index);
maxModified = size();
modified = true;
flush();
} catch (IndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException(e.getMessage());
}
}
@Override
public T remove(int index) {
try {
T toRet = list.remove(index);
minModified = Math.min(minModified, index);
maxModified = size();
modified = true;
flush();
return toRet;
} catch (IndexOutOfBoundsException e) {
throw new IndexOutOfBoundsException(e.getMessage());
}
}
@Override
public boolean remove(Object o) {
int index = indexOf(o);
if (index == -1) {
return false;
}
remove(index);
return true;
}
public void setList(List<T> listToWrap) {
listWrapper = new ListWrapper(listToWrap);
listWrapper.minModified = 0;
listWrapper.maxModified = listWrapper.size();
listWrapper.modified = true;
flush();
}
public List<T> getList() {
return listWrapper;
}
/**
*构造一个新的{@link ListWrapper},将flush调用委托给
*指定的委托。
*
*@param列出要包装的列表
*@param delegate代理
*@param offset此列表的偏移量
*/
私有ListWrapper(列表列表、ListWrapper委托、int偏移量){
this.list=列表;
this.delegate=委托;
这个偏移量=偏移量;
}
@凌驾
公共空添加(整数索引,T元素){
试一试{
添加(索引、元素);
minModified=Math.min(minModified,index);
maxModified=size();
修改=真;
冲洗();
}catch(IndexOutOfBoundsException e){
抛出新的IndexOutOfBoundsException(例如getMessage());
}
}
@凌驾
公共T删除(整型索引){
试一试{
T toRet=list.remove(索引);
minModified=Math.min(minModified,index);
maxModified=size();
修改=真;
冲洗();
返回托雷特;
}catch(IndexOutOfBoundsException e){
抛出新的IndexOutOfBoundsException(例如getMessage());
}
}
@凌驾
公共布尔删除(对象o){
int index=indexOf(o);
如果(索引==-1){
返回false;
}
删除(索引);
返回true;
}
公共无效集合列表(列表列表集合){
listWrapper=新的listWrapper(listToWrap);
listWrapper.minModified=0;
listWrapper.maxModified=listWrapper.size();
listWrapper.modified=true;
冲洗();
}
公共列表getList(){
返回列表包装器;
}