具有相同超类的子对象列表的列表
我正在尝试设计一个模型,其中有一个具有相同超类的子对象列表。该模型将是 的模型列表视图。超类是:
public class Element implements Serializable {
private static final long serialVersionUID = 10121L;
private String value;
public Element(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Element [value=" + value + "]";
}
}
子类之一是:
public class DateElement extends Element {
private static final long serialVersionUID = 10122L;
private DateTime date;
public DateElement(String value, DateTime date) {
super(value);
this.date = date;
}
public DateTime getDate() {
return date;
}
public void setDate(DateTime date) {
this.date = date;
}
@Override
public String toString() {
return "DateElement [date=" + date + ", value=" + getValue() + "]";
}
}
我将列表的列表实例化为:
this.userMonitorMap = new ArrayList<List<? extends Element>>(0);
在 userMonitorMap 中添加 DateElement 的列表为:
List<DateElement> dateElements = getDateElements();
userMonitorMap.add(dateElements);
到目前为止,我没有遇到任何问题。但是ListView的填充有问题,代码片段是:
ListView row = new ListView("row", userMonitorMap) {
@Override
protected void populateItem(ListItem rowItem) {
List<? extends Element> columnMap = (List<? extends Element>) rowItem;
ListView column = new ListView("column", columnMap) {
@Override
protected void populateItem(ListItem columnItem) {
Element element = (Element) columnItem; //The compile time error is here
}
};
rowItem.add(column);
}
};
我已经在代码中评论了这个问题。我怎样才能实现这个目标?我需要做出哪些改变?
Eclipse IDE 红色标记了该行,错误消息为:无法从 ListItem 转换为 Element
谢谢和问候。
I am trying to design a model in which I have a List of List of child object having same super class. This model will be a Model of a ListView. The super class is:
public class Element implements Serializable {
private static final long serialVersionUID = 10121L;
private String value;
public Element(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Element [value=" + value + "]";
}
}
One of the sub class is:
public class DateElement extends Element {
private static final long serialVersionUID = 10122L;
private DateTime date;
public DateElement(String value, DateTime date) {
super(value);
this.date = date;
}
public DateTime getDate() {
return date;
}
public void setDate(DateTime date) {
this.date = date;
}
@Override
public String toString() {
return "DateElement [date=" + date + ", value=" + getValue() + "]";
}
}
I am instantiating the List of List as:
this.userMonitorMap = new ArrayList<List<? extends Element>>(0);
Adding a List of DateElement in userMonitorMap as:
List<DateElement> dateElements = getDateElements();
userMonitorMap.add(dateElements);
Upto this portion I am having no trouble. But the population of the ListView has problem, the code snippet is:
ListView row = new ListView("row", userMonitorMap) {
@Override
protected void populateItem(ListItem rowItem) {
List<? extends Element> columnMap = (List<? extends Element>) rowItem;
ListView column = new ListView("column", columnMap) {
@Override
protected void populateItem(ListItem columnItem) {
Element element = (Element) columnItem; //The compile time error is here
}
};
rowItem.add(column);
}
};
I have commented the problem in my code. How can I achieve this? What are the changes do I need?
The eclipse IDE red marked that line and the error message is: Cannot cast from ListItem to Element
Thanks and Regards.
ListItem
不是列表本身中的对象,它包含一个包含您的对象的模型。 应该使用:或者
看看这些示例
您 代码应为:
ListView row = new ListView("row", userMonitorMap) {
The
ListItem
isn't the object in the list itself, it contains a model which contains your object. You should be using:or
Take a look at these examples
Your code should read:
ListView row = new ListView("row", userMonitorMap) {