Java Android上的简单XML—类MyArrayList<;E>;扩展ArrayList<;E>;?
我不熟悉简单的xml库。我真的很喜欢,但我有个问题 以下是我的类(一些代码已被删除以使其更简洁): , 当我实例化MyArrayList并添加一些VideoEntries并调用ToXml时,我只得到一个空的根ieJava Android上的简单XML—类MyArrayList<;E>;扩展ArrayList<;E>;?,java,android,xml,Java,Android,Xml,我不熟悉简单的xml库。我真的很喜欢,但我有个问题 以下是我的类(一些代码已被删除以使其更简洁): , 当我实例化MyArrayList并添加一些VideoEntries并调用ToXml时,我只得到一个空的根ie <MyArrayList /> 我如何解决这个问题?这与MyArrayList是泛型的这一事实有关吗?列表必须是元素的成员(并且没有单独的类)才能获得所需的行为,您可以将ElementList设置为内联,因此没有父元素 @Root public class MyArr
<MyArrayList />
我如何解决这个问题?这与MyArrayList是泛型的这一事实有关吗?列表必须是元素的成员(并且没有单独的类)才能获得所需的行为,您可以将ElementList设置为内联,因此没有父元素
@Root
public class MyArrayList<E> {
@ElementList(inline=true)
ArrayList<E> list = new ArrayList<E>();
public boolean add(E entry) {
return list.add(entry);
}
public void ToXml() throws Exception {
Serializer serializer = new Persister();
File file = new File("somewhere in my file system");
serializer.write(this, file);
}
}
BTW,如果你只是开始编程java,你可能会考虑阅读java代码约定——用大写字母来启动方法和变量(不是习惯坏习惯)不是一个好的做法(-----)。您能建议一种方法吗?这是我得到org.simpleframework.xml.core.PersistenceException的错误:类的构造函数不匹配VideoEntry@PhilippWendt我试图使用它,但当我尝试反序列化类型E时,它总是一个对象,而不是列表的真实类型。我可能做错了什么?
public class VideoEntry extends MediaEntry {
private static final long serialVersionUID = 1L;
public VideoEntry(String title, String description,
String url, String localPath) {
super(title, description, url, localPath);
}
}
<MyArrayList />
@Root
public class MyArrayList<E> {
@ElementList(inline=true)
ArrayList<E> list = new ArrayList<E>();
public boolean add(E entry) {
return list.add(entry);
}
public void ToXml() throws Exception {
Serializer serializer = new Persister();
File file = new File("somewhere in my file system");
serializer.write(this, file);
}
}
@Root
public class MyArrayList<E> extends ArrayList<E> {
@ElementList(inline=true)
MyArrayList<E> list = this;
public void ToXml() throws Exception {
Serializer serializer = new Persister();
File file = new File("somewhere in my file system");
serializer.write(this, file);
}
}
@Root
public abstract class MediaEntry implements Serializable {
private static final long serialVersionUID = 1L;
@Element
public String Title;
@Element
public String Description;
@Element
public String Url;
@Element
public String LocalPath;
public MediaEntry(@Element(name = "Title") String title,
@Element(name = "Description") String description,
@Element(name = "Url") String url,
@Element(name = "LocalPath") String localPath) {
Title = title;
Description = description;
Url = url;
LocalPath = localPath;
}
}