public class Author implements Serializable{
private int id;
private String name;
//...
}
public class Book implements Serializable{
private String title;
private Author author;
//...
}
Book book=new Book();
book.setTitle("Java编程思想");
Author author=new Author();
author.setId(1);
author.setName("Bruce Eckel");
book.setAuthor(author);
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("book",book);
startActivity(intent);
Book book= (Book) getIntent().getSerializableExtra("book");
Log.d(TAG,"book title->"+book.getTitle());
Log.d(TAG,"book author name->"+book.getAuthor().getName());
public class Author{
private int id;
private String name;
//...
}
public class Book{
private String title;
private Author author;
//...
}
Book book=new Book();
book.setTitle("Java编程思想");
Author author=new Author();
author.setId(1);
author.setName("Bruce Eckel");
book.setAuthor(author);
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("book",new Gson().toJson(book));
startActivity(intent);
String bookJson=getIntent().getStringExtra("book");
Book book=new Gson().fromJson(bookJson,Book.class);
Log.d(TAG,"book title->"+book.getTitle());
Log.d(TAG,"book author name->"+book.getAuthor().getName());
public class Author implements Parcelable{
private int id;
private String name;
//setter & getter...
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
//该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存,
// 以便从parcel容器获取数据
dest.writeString(name);
dest.writeInt(id);
}
public static final Creator<Author> CREATOR=new Creator<Author>() {
@Override
public Author createFromParcel(Parcel source) {
//从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。
Author author=new Author();
author.setName(source.readString());
author.setId(source.readInt());
return author;
}
@Override
public Author[] newArray(int size) {
//创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。方法是供外部类反序列化本类数组使用。
return new Author[size];
}
};
}
public class Book implements Parcelable{
private String title;
private Author author;
//setter & getter...
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeParcelable(author,flags);
}
public static final Creator<Book> CREATOR=new Creator<Book>() {
@Override
public Book createFromParcel(Parcel source) {
Book book=new Book();
book.setTitle(source.readString());
book.setAuthor(source.<Author>readParcelable(Author.class.getClassLoader()));
return book;
}
@Override
public Book[] newArray(int size) {
return new Book[0];
}
};
}
Book book=new Book();
book.setTitle("Java编程思想");
Author author=new Author();
author.setId(1);
author.setName("Bruce Eckel");
book.setAuthor(author);
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("book",book);
startActivity(intent);
Book book=getIntent().getParcelableExtra("book");
Log.d(TAG,"book title->"+book.getTitle());
Log.d(TAG,"book author name->"+book.getAuthor().getName());
@Parcel
public class Author {
int id;
String name;
//setter & getter...
}
@Parcel
public class Book {
String title;
Author author;
//setter & getter
}
Book book=new Book();
book.setTitle("Java编程思想");
Author author=new Author();
author.setId(1);
author.setName("Bruce Eckel");
book.setAuthor(author);
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("book", Parcels.wrap(book));
startActivity(intent);
Book book= Parcels.unwrap(getIntent().getParcelableExtra("book"));
Log.d(TAG,"book title->"+book.getTitle());
Log.d(TAG,"book author name->"+book.getAuthor().getName());
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有