直接开正题,Intent传递值就是平常那些很简单的,接下来介绍传递 对象,和 对象数组
1 .intent 传递自定义的 对象 -
实体类继承 Serializable
- public class AA implements Serializable{
-
- public String a;
- public String aa;
- public String aaa;
-
-
- }
intent塞入数据端
- AA a = new AA();
- a.a="a";
- a.aa ="aa";
- a.aaa = "aaa";
-
- Intent in = new Intent().setClass(MainActivity.this, MainActivity1.class);
- Bundle bu = new Bundle();
- bu.putSerializable("enty", a);
- in.putExtras(bu);
- startActivity(in);
intent 接收数据端
- AA a =(AA)this.getIntent().getExtras().getSerializable("enty");
- System.out.println("a.a ="+ app.a);
- System.out.println("a.aa ="+ app.aa);
- System.out.println("a.aaa ="+ app.aaa);
实体类类继承 Parcelable(简单介绍一下:
1.什么是Parcelable ?
Parcelable,定义了将数据写入Parcel,和从Parcel中读出的接口。一个实体(用类来表示),如果需要封装到消息中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。
2.Parcelable 传递对象
Android序列化对象主要有两种方法:
1.1实现Serializable接口是JavaSE本身就支持的;
1.2实现Parcelable接口,Parcelable是Android特有的功能,效率比实现Serializable接口高,像用于Intent数据传递也都支持,而且还可以用在进程间通信(IPC),
除了基本类型外,只有实现了Parcelable接口的类才能被放入Parcel中。
)
Parcelable的接口的定义:
- public interface Parcelable {
-
- public int describeContents();
-
- public void writeToParcel(Parcel dest, int flags);
-
-
- public interface Creator<T> {
- public T createFromParcel(Parcel source);
- public T[] newArray(int size);
- }
- public class AA implements Parcelable{
-
- public String a;
- public String aa;
- public String aaa;
-
- @Override
- public int describeContents() {
- return 0;
- }
-
- @Override
- public void writeToParcel(Parcel parcel, int flags) {
-
- parcel.writeString(a);
- parcel.writeString(aa);
- parcel.writeString(aaa);
-
- }
-
- public static final Parcelable.Creator<AA> CREATOR = new Creator<AA>(){
- @Override
- public AA createFromParcel(Parcel source) {
- AA app= new AA();
- app.a = source.readString();
- app.aa = source.readString();
- app.aaa = source.readString();
- return app;
- }
- @Override
- public AA[] newArray(int size) {
- return new AA[size];
- }
-
- };
- }
intent塞入数据端
- AA a = new AA();
- a.a="a";
- a.aa ="aa";
- a.aaa = "aaa";
-
- Intent in = new Intent().setClass(MainActivity.this, MainActivity1.class);
- Bundle bu = new Bundle();
- bu.putParcelable("enty", a);
- in.putExtras(bu);
- startActivity(in);
接收数据端
-
- app =(AA)this.getIntent().getExtras().getParcelable("enty");
- System.out.println("a.a ="+ app.a);
- System.out.println("a.aa ="+ app.aa);
- System.out.println("a.aaa ="+ app.aaa);
2。 下来是 自定义对象 数组传递
实体类类继承 Parcelable
intent塞入数据端
- List<AA> list_aa =new ArrayList<AA>();
- AA a = new AA();
- a.a="a";
- a.aa ="aa";
- a.aaa = "aaa";
-
- AA a1 = new AA();
- a1.a="a1";
- a1.aa ="aa1";
- a1.aaa = "aaa1";
-
- list_aa.add(a);
- list_aa.add(a1);
-
- Intent in = new Intent().setClass(MainActivity.this, MainActivity1.class);
- Bundle bu = new Bundle();
- bu.putParcelableArrayList("list", (ArrayList<? extends Parcelable>) list_aa);
- in.putExtras(bu);
- startActivity(in);
接收list数据端
- List<AA> aa =this.getIntent().getExtras().getParcelableArrayList("list");
-
- for (AA aa2 : aa) {
- System.out.println("list_a.a = "+ aa2.a);
- System.out.println("list_a.aa = "+ aa2.aa);
- System.out.println("list_a.aaa = "+ aa2.aaa);
- System.out.println("=================================");
-
- }