具有相同超类的子对象列表的列表

发布于 11-07 13:21 字数 2341 浏览 3 评论 0原文

我正在尝试设计一个模型,其中有一个具有相同超类的子对象列表。该模型将是 的模型列表视图。超类是:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ListItem 不是列表本身中的对象,它包含一个包含您的对象的模型。 应该使用:

Model<Element> elementModel = columnItem.getModel();

或者

Element element = columnItem.getModelObject();

看看这些示例

您 代码应为:

ListView row = new ListView("row", userMonitorMap) {

        @Override
        protected void populateItem(ListItem rowItem) {
            List<? extends Element> columnMap = rowItem.getModelObject();
            ListView column = new ListView("column", columnMap) {

                @Override
                protected void populateItem(ListItem columnItem) {
                    Element element = columnItem.getModelObject();
                }
            };
            rowItem.add(column);
        }
    };

The ListItem isn't the object in the list itself, it contains a model which contains your object. You should be using:

Model<Element> elementModel = columnItem.getModel();

or

Element element = columnItem.getModelObject();

Take a look at these examples

Your code should read:

ListView row = new ListView("row", userMonitorMap) {

        @Override
        protected void populateItem(ListItem rowItem) {
            List<? extends Element> columnMap = rowItem.getModelObject();
            ListView column = new ListView("column", columnMap) {

                @Override
                protected void populateItem(ListItem columnItem) {
                    Element element = columnItem.getModelObject();
                }
            };
            rowItem.add(column);
        }
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文