传递和ArrayList< Service>通过意图 [英] Passing and ArrayList<Service> through intent

查看:122
本文介绍了传递和ArrayList< Service>通过意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为Service的类,该类用于使用此构造函数创建Service对象

I have a class called Service, which is used to create Service objects using this constructor

public Service(int id, String service_name, String service_code) {
    this.id = id;
    this.service_name = service_name;
    this.service_code = service_code;
}

然后我创建带有以下签名的列表呼叫服务列表

then I create a list call service list as with the following signature

List<Service> serviceList = new ArrayList<Service>

我试图通过这样的Intent对象传递此ArrayList

I have try to pass this ArrayList through Intent Object like this

Intent i = new Intent(Classname.this, anotherClass.class);
i.putExtras("serviceList",serviceList);
startActivity(i);

但是失败了.我通过ArrayList对象传递意图的方式是什么.

But it fails. What is the way I pass through intent with ArrayList object.

推荐答案

您的自定义类必须实现ParcelableSerializable以便在Intent中进行序列化/反序列化.

Your custom class has to implement Parcelable or Serializable in order to serialize/de-serialize within an Intent.

您的类Service必须看起来像这样(使用了生成器 http://www.parcelabler. com/)

Your class Service has to look like this for example (used a generator http://www.parcelabler.com/)

public class Service implements Parcelable {
private int id;
private String service_name;
private String service_code;
public Service(int id, String service_name, String service_code) {
this.id = id;
this.service_name = service_name;
this.service_code = service_code;
}


protected Service(Parcel in) {
    id = in.readInt();
    service_name = in.readString();
    service_code = in.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(service_name);
    dest.writeString(service_code);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<Service> CREATOR = new Parcelable.Creator<Service>() {
    @Override
    public Service createFromParcel(Parcel in) {
        return new Service(in);
    }

    @Override
    public Service[] newArray(int size) {
        return new Service[size];
    }
};

}

然后您可以将getIntent().getParcelableArrayListExtra()用于投射

ArrayList<Service> serviceList= intent.<Service>getParcelableArrayList("list"));

对于发送,您可以这样使用

For sending you use it like this

intent.putParcelableArrayListExtra("list", yourServiceArrayList);

请注意,yourServiceArrayList应该是ArrayList

Note that the yourServiceArrayList should be an ArrayList

如果是列表,则可以通过

if it is List the you can pass through

intent.putParcelableArrayListExtra("list", (ArrayList<? extends Parcelable>) yourServiceArrayList);

intent.putParcelableArrayListExtra("list", (ArrayList<? extends Parcelable>) yourServiceArrayList);

这篇关于传递和ArrayList&lt; Service&gt;通过意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆