Android中传递对象的三种方法
来源:互联网 发布:网络大v引导舆情走向 编辑:程序博客网 时间:2024/05/06 00:26
Android中,Activity和Fragment之间传递对象,可以通过将对象序列化并存入Bundle或者Intent中进行传递,也可以将对象转化为JSON字符串,进行传递。
序列化对象可以使用Java的Serializable
的接口、Parcelable
接口。转化成JSON字符串,可以使用Gson
等库。
1.Serializable
Modelpublic class Author implements Serializable{
private int id;
private String name;
//...
}
1
2
3
4
5
public class Book implements Serializable{
private String title;
private Author author;
//...
}
传递数据
1
2
3
4
5
6
7
8
9
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());
2.转化为JSON字符串
Model1
2
3
4
5
6
7
8
9
10
11
12
public class Author{
private int id;
private String name;
//...
}
public class Book{
private String title;
private Author author;
//...
}
传递数据
1
2
3
4
5
6
7
8
9
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);
接收数据
1
2
3
4
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());
3.使用Parcelable
实现Parcelable
接口需要实现两个方法
describeContents方法。内容接口描述,默认返回0就可以;
writeToParcel方法。将传递的数据打包到Parcel容器中。
除了要实现这两个方法还必须创建一个Parcelable.Creator
接口的实例,用于读取Parcel容器中的数据
Model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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];
}
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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];
}
};
}
1
2
3
4
5
6
7
8
9
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);
接收数据
1
2
3
Book book=getIntent().getParcelableExtra(
"book"
);
Log.d(TAG,
"book title->"
+book.getTitle());
Log.d(TAG,
"book author name->"
+book.getAuthor().getName());
4.性能分析
经过测试,我们得到下图的效果
可以看出,通过转换为字符串的速度是最慢的。Seralizable次之,Parcelable比Seralizable快10倍。所以从性能上考 虑,我们必定优先选择Parcelable。
0 0
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android中传递对象的三种方法
- Android -- Intent传递对象的三种方法
- Android -- Intent传递对象的三种方法
- Android中传递对象的三种方法以及Java对象的序列化和反序列化实践
- Android中Intent传递对象的两种方法!
- android中Intent传递对象的实现方法
- Android中Intent传递Java对象的方法
- Android中Intent传递类对象的方法一(Serializable)
- Android中Intent传递类对象的方法二(Parcelable)
- android中intent传递list或者对象的方法
- Shell脚本实现每个工作日定时执行指定程序(学习记录)
- 在Github和Git上fork之简单指南
- GUN学习笔记之make变量
- 医疗博文锚文本
- Linux makefile 教程 非常详细,且易懂
- Android中传递对象的三种方法
- mimetype1
- caret包应用之一:数据预处理
- eclipse hibernate tools安装和入门使用
- Jsoup使用指南
- C#中委托和事件的区别实例解析
- Android编译系统
- 转载goosman的励志经历,自励共勉!
- IP地址、网络号、主机号、广播地址等分析计算