在 p:datatable 中使用自定义 collections
Using custom collections in p:datatable
我正在尝试为我的 p:datatable
使用自定义 collection
自定义 Collection:
public class ManagedArrayList<E> extends java.util.ArrayList<E> {
private static final int DEFAULT_SIZE_LIMIT = 0; //unlimited
private ArrayList<E> internalList;
private int maxSize;
public ManagedArrayList(int maxSize) {
this.maxSize = maxSize;
internalList = new ArrayList<E> ();
}
public ManagedArrayList() {
this(DEFAULT_SIZE_LIMIT);
}
@Override
public boolean add(E objectToAdd){
if(maxSize>0){
if(internalList.size()>maxSize){
throw new IllegalStateException("List is full");
}
}
return internalList.add(objectToAdd);
}
@Override
public E get(int index){
return internalList.get(index);
}
@Override
public E remove(int index){
return internalList.remove(index);
}
@Override
public boolean remove(Object o){
return internalList.remove(o);
}
@Override
public Iterator<E> iterator(){
return internalList.iterator();
}
public List<E> toList(){
return (List)internalList;
}
}
然而,当我按原样使用它时,我收到消息:"No records found."
如果我先将我的自定义列表转换为 <List>
,它就可以正常工作。
转换我的列表:
private ManagedArrayList<FragmentTuple> fragments = new ManagedArrayList();
public List<FragmentTuple> getUICompatibleFragments() {
List<FragmentTuple> f = this.fragments.toList();
return f;
}
我更愿意直接使用我的自定义 collection 而无需转换。我在 ManagedArrayList 的实现中是否缺少某些东西,以便它可以在 p:datatable 中工作,最好也在 ui:repeat
中工作
您忘记覆盖其他 size()
and isEmpty()
methods. Moreover, you're never delegating to ArrayList
superclass, but keeping an internal ArrayList
instance. One would wonder why you extend from ArrayList
at all instead of AbstractList
or even List
,这反过来会自动强制您实施正确的方法。因此,另一种方法是委托给 ArrayList
超类而不是拥有一个内部实例,这样 size()
和 isEmpty()
调用将 "automagically" return 正确值。
我正在尝试为我的 p:datatable
使用自定义 collection自定义 Collection:
public class ManagedArrayList<E> extends java.util.ArrayList<E> {
private static final int DEFAULT_SIZE_LIMIT = 0; //unlimited
private ArrayList<E> internalList;
private int maxSize;
public ManagedArrayList(int maxSize) {
this.maxSize = maxSize;
internalList = new ArrayList<E> ();
}
public ManagedArrayList() {
this(DEFAULT_SIZE_LIMIT);
}
@Override
public boolean add(E objectToAdd){
if(maxSize>0){
if(internalList.size()>maxSize){
throw new IllegalStateException("List is full");
}
}
return internalList.add(objectToAdd);
}
@Override
public E get(int index){
return internalList.get(index);
}
@Override
public E remove(int index){
return internalList.remove(index);
}
@Override
public boolean remove(Object o){
return internalList.remove(o);
}
@Override
public Iterator<E> iterator(){
return internalList.iterator();
}
public List<E> toList(){
return (List)internalList;
}
}
然而,当我按原样使用它时,我收到消息:"No records found."
如果我先将我的自定义列表转换为 <List>
,它就可以正常工作。
转换我的列表:
private ManagedArrayList<FragmentTuple> fragments = new ManagedArrayList();
public List<FragmentTuple> getUICompatibleFragments() {
List<FragmentTuple> f = this.fragments.toList();
return f;
}
我更愿意直接使用我的自定义 collection 而无需转换。我在 ManagedArrayList 的实现中是否缺少某些东西,以便它可以在 p:datatable 中工作,最好也在 ui:repeat
中工作您忘记覆盖其他 size()
and isEmpty()
methods. Moreover, you're never delegating to ArrayList
superclass, but keeping an internal ArrayList
instance. One would wonder why you extend from ArrayList
at all instead of AbstractList
or even List
,这反过来会自动强制您实施正确的方法。因此,另一种方法是委托给 ArrayList
超类而不是拥有一个内部实例,这样 size()
和 isEmpty()
调用将 "automagically" return 正确值。