Parcelable继承:抽象类 - 哪些CREATOR? [英] Parcelable inheritance: abstract class - Which CREATOR?
问题描述
我有一个抽象类 A
它实现Parcelable。
我有一个类 B
和类 C
谁都扩展 A
。
我怎样才能让他们parcelable?
因为我可以链,并提供了一个创作者
无论是在 A
和 B
类似建议的很多帖子。但因为我有谁保存农行
类和实施Parcelable themselfes其他对象,这种做法似乎并不奏效,因为当我想通过 A
我将不得不
的ArrayList< A>元素=新的ArrayList<>();
in.readTypedList(元素,B.CREATOR); // B.CREATOR? C.CREATOR ???
这显然是没有意义的。所以,我怎么能正确地做出一个Parcelable?
即我想使这个类Parcelable,所以我可以指的是在一个共同的方式。
A)
公共抽象类的一个实现Parcelable {
最后弦乐globalVar;
公开发行A(字符串globalVar){
this.globalVar = globalVar;
}
}
B)
公共类B扩展A {
字符串BVAR;
市民B(字符串全球,字符串BVAR){
超级(全局);
this.bVar = BVAR;
}
私人B(小包){
超级(in.readString());
this.bVar = in.readString();
}
@覆盖
公众诠释describeContents(){
返回0;
}
@覆盖
公共无效writeToParcel(包裹包裹,int i)以{
parcel.writeString(BVAR);
}
}
C)
公共C类扩展了A {
字符串CVaR的;
市民C(字符串全球,字符串CVAR){
超级(全局);
this.cVar = CVaR的;
}
@覆盖
公众诠释describeContents(){
返回0;
}
@覆盖
公共无效writeToParcel(包裹包裹,int i)以{
parcel.writeString(CVAR);
}
}
我用文森特·米蒙 - 普拉特的这一职位parcelable架构: Parcelable和继承Android中并遇到同样的类型化列出了问题,他们不得不指定一个不可能抽象的创造者。
在搜索中包的Javadoc我找到了方法的写列表(表VAL)的内部使用<一个href="http://developer.android.com/reference/android/os/Parcel.html#writeValue%28java.lang.Object%29"相对=nofollow> writeValue(对象)法列表中的每个对象,因此调用 writeToParcel()
从子类(B和C从A名单)。解组列表使用其对应的<一个href="http://developer.android.com/reference/android/os/Parcel.html#readList%28java.util.List,%20java.lang.ClassLoader%29"相对=nofollow> readList(名单outVal,类加载器加载器)其中 A的ClassLoader
有传递或者 android.os。 BadParcelableException
被抛出,至少在我的情况。
含有A元素列表类应该是这样的:
公共类的aContainer实现Parcelable {
//其他的aContainer领域
名单&LT; A&GT;元素=新的ArrayList&LT;&GT;();
//其他的aContainer领域
静态最终Parcelable.Creator&LT;的aContainer&GT; CREATOR =新Parcelable.Creator&LT;的aContainer&GT;(){
@覆盖
公众的aContainer createFromParcel(宗源){
返回新的aContainer(源);
}
@覆盖
公众的aContainer [] newArray(INT尺寸){
返回新的aContainer【尺寸】;
}
};
公众的aContainer(){
}
受保护的aContainer(宗源){
//读取其他的aContainer领域
source.readList(元素,A.class.getClassLoader());
//读取其他的aContainer领域
}
@覆盖
公众诠释describeContents(){
返回0;
}
@覆盖
公共无效writeToParcel(包裹DEST,INT标志){
//写其他的aContainer领域
dest.writeList(元素);
//写其他的aContainer领域
}
}
使用这些方法可能会比 readTypedList()
和 writeTypedList()
但选自B具体数据有点慢和C子类也瓜分,而不是仅仅从一个抽象的超类的字段(这将是不可能存在抽象的)。您可以从B和C追偿实例。
I have an abstract class A
which implements Parcelable.
I have a class B
and a class C
who both extend A
.
How can I make them parcelable?
Of cause I could chain it and provide a CREATOR
both in A
and B
like suggested in many posts. But since I have other Objects who store the A-B-C
classes and implement Parcelable themselfes, that approach seems not to be working because when I want to pass an ArrayList of A
I would have to use the CREATOR
in the typed list by
ArrayList<A> elements = new ArrayList<>();
in.readTypedList(elements , B.CREATOR); // B.CREATOR? C.CREATOR???
Which obviously makes no sense. So how can I properly make A Parcelable?
I.e I want to make this class Parcelable so I can refer to A in a common way.
A)
public abstract class A implements Parcelable {
final String globalVar;
public A(String globalVar) {
this.globalVar = globalVar;
}
}
B)
public class B extends A {
String bVar;
public B(String global, String bVar) {
super(global);
this.bVar = bVar;
}
private B(Parcel in) {
super(in.readString());
this.bVar = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(bVar);
}
}
C)
public class C extends A {
String cVar;
public C(String global, String cVar) {
super(global);
this.cVar = cVar;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(cVar);
}
}
I used Vincent Mimoun-Prat's parcelable architecture from this post: Parcelable and inheritance in Android and ran into the same typed list problem about having to specify an impossible abstract CREATOR.
Searching in Parcel's JavaDoc I found the method writeList(List val) that internally uses writeValue(Object) method for each object of the list and therefore calling writeToParcel()
from subclasses (B and C from an A list). To unmarshall the list use its counterpart readList (List outVal, ClassLoader loader) where A ClassLoader
has to be passed or an android.os.BadParcelableException
is thrown, at least in my case.
Class containing A elements list should be something like this:
public class AContainer implements Parcelable {
//Other AContainer fields
List<A> elements = new ArrayList<>();
//Other AContainer fields
static final Parcelable.Creator<AContainer> CREATOR = new Parcelable.Creator<AContainer>() {
@Override
public AContainer createFromParcel(Parcel source) {
return new AContainer(source);
}
@Override
public AContainer[] newArray(int size) {
return new AContainer[size];
}
};
public AContainer() {
}
protected AContainer(Parcel source) {
//read other AContainer fields
source.readList(elements, A.class.getClassLoader());
//read other AContainer fields
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//write other AContainer fields
dest.writeList(elements);
//write other AContainer fields
}
}
Using these methods may be a bit slower than readTypedList()
and writeTypedList()
but specific data from B and C subclasses is also "parcelled" and not only the fields from A abstract superclass (It will be impossible being abstract). You recover the right instances from B and C.
这篇关于Parcelable继承:抽象类 - 哪些CREATOR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!