Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中使通用arraylist可打包?_Java_Android_Parcelable - Fatal编程技术网

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];
    }
};