Java 如何在Android中使通用arraylist可打包?
我有这样一个类:(可以找到完整的定义) 但是构造函数会给我Java 如何在Android中使通用arraylist可打包?,java,android,parcelable,Java,Android,Parcelable,我有这样一个类:(可以找到完整的定义) 但是构造函数会给我BadParcelableException 我也试过: protected AList(Parcel in) { in.readList(list, T.class.getClassLoader()); } 但是T不能用作变量,因此我不知道如何修复此语法。为了使列表可以打包,该列表的每个元素都应该实现可打包。因此,基本上您可以将列表定义为ArrayList List=new ArrayList() 几个测试: list.add
BadParcelableException
我也试过:
protected AList(Parcel in) {
in.readList(list, T.class.getClassLoader());
}
但是
T
不能用作变量,因此我不知道如何修复此语法。为了使列表
可以打包,该列表
的每个元素都应该实现可打包
。因此,基本上您可以将列表定义为ArrayList List=new ArrayList()
几个测试:
list.add("test"); // error, String doesn't implement Parcelable
list.add(new SomeClass()); // fine
list.add(new android.location.Address(Locale.getDefault())); // fine
这通常用于将数据附加到Intent
s,例如:
Intent i = new Intent(this, SomeActivity.class)
i.putParcelableArrayListExtra("key", list);
为了使您的
列表
可打包,该列表
的每个元素都应实现可打包
。因此,基本上您可以将列表定义为ArrayList List=new ArrayList()
几个测试:
list.add("test"); // error, String doesn't implement Parcelable
list.add(new SomeClass()); // fine
list.add(new android.location.Address(Locale.getDefault())); // fine
这通常用于将数据附加到Intent
s,例如:
Intent i = new Intent(this, SomeActivity.class)
i.putParcelableArrayListExtra("key", list);
我已经放弃将
AList
实现为Parcelable
。但是,由于AList
类似于ArrayList
,因此当我需要将AList
写入包裹时,我可以将其完全视为ArrayList
例如,我在Food
类中声明了一个AList标签。所以在保护食品(包裹内)
中,我可以写:
ArrayList<Tag> tags = new ArrayList<>();
in.readTypedList(tags, Tag.CREATOR);
Tags = new AList<>(tags);
我的Food
类的源代码是。我已经放弃将AList
作为包裹实现。但是,由于AList
类似于ArrayList
,因此当我需要将AList
写入包裹时,我可以将其完全视为ArrayList
例如,我在Food
类中声明了一个AList标签。所以在保护食品(包裹内)
中,我可以写:
ArrayList<Tag> tags = new ArrayList<>();
in.readTypedList(tags, Tag.CREATOR);
Tags = new AList<>(tags);
我的食品类的源代码是。公共类示例实现了Parcelable{
数组列表;
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的写入列表(此列表);
}
公共样本(){
}
受保护样品(包裹中){
this.list=new ArrayList();
in.readList(this.list,T.class.getClassLoader());
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共示例createFromParcel(地块源){
返回新样品(来源);
}
@凌驾
公共样本[]新数组(整数大小){
返回新样本[大小];
}
};
}公共类示例实现可包裹{
数组列表;
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的写入列表(此列表);
}
公共样本(){
}
受保护样品(包裹中){
this.list=new ArrayList();
in.readList(this.list,T.class.getClassLoader());
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共示例createFromParcel(地块源){
返回新样品(来源);
}
@凌驾
公共样本[]新数组(整数大小){
返回新样本[大小];
}
};
}T.class.getClassLoader()
将不会像问题中所解释的那样编译。T.class.getClassLoader()
将不会像问题中所解释的那样编译。
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(Name);
dest.writeString(ImagePath);
dest.writeTypedList(Tags.ToArrayList());
dest.writeString(Note);
dest.writeByte((byte) (IsFavorite ? 1 : 0));
if (DateAdded == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeLong(DateAdded);
}
}
public class Sample<T> implements Parcelable {
ArrayList<T> list;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(this.list);
}
public Sample() {
}
protected Sample(Parcel in) {
this.list = new ArrayList<T>();
in.readList(this.list, T.class.getClassLoader());
}
public static final Parcelable.Creator<Sample> CREATOR = new Parcelable.Creator<Sample>() {
@Override
public Sample createFromParcel(Parcel source) {
return new Sample(source);
}
@Override
public Sample[] newArray(int size) {
return new Sample[size];
}
};