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.
The
ListItemisn’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) {