1.对要传递的对象类型实现Parcelable:
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
39
40
41
42
43
44
45
46
|
public
class
User
implements
Parcelable
//对象必须实现Parcelable接口才可以putExtra(或者序列化)
{
String name;
String psw;
public
User(String name, String psw)
{
super
();
this
.name = name;
this
.psw = psw;
}
@Override
public
String toString()
{
return
"User [name="
+ name +
", psw="
+ psw +
"]"
;
}
@Override
public
int
describeContents()
{
return
0
;
}
@Override
public
void
writeToParcel(Parcel dest,
int
flags)
//将属性写入Parcel对象
{
dest.writeString(name);
dest.writeString(psw);
}
public
static
final
Parcelable.Creator<User> CREATOR =
new
Parcelable.Creator<User>()
{
public
User createFromParcel(Parcel in)
{
return
new
User(in);
//创建一个有参构造函数
}
public
User[] newArray(
int
size)
{
return
new
User[size];
}
};
public
User(Parcel in)
//根据写入的顺序依次读取
{
name = in.readString();
psw = in.readString();
}
}
|
2.第一个activity传递user对象:
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
|
public
class
MainActivity
extends
Activity
{
private
EditText editText1;
private
EditText editText2;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
findViewById(R.id.button1).setOnClickListener(
new
OnClickListener()
{
@Override
public
void
onClick(View v)
{
String namestr = editText1.getText().toString();
String pswstr = editText2.getText().toString();
User user =
new
User(namestr, pswstr);
Intent intent =
new
Intent(MainActivity.
this
, NextActivity.
class
);
intent.putExtra(
"user"
, user);
//传入对象,对象类必须实现Parcelable或者序列化
startActivity(intent);
}
});
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
}
|
3.第二个activity接收user对象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public
class
NextActivity
extends
Activity
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
TextView textView = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
User user = intent.getParcelableExtra(
"user"
);
//得到一个对象
textView.setText(user.toString());
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_next, menu);
return
true
;
}
}
|
其他笔记:
1.带有数组类型属性的类
1
2
3
4
5
6
7
8
9
10
|
@Override
public
void
writeToParcel(Parcel dest,
int
flags)
{
dest.writeStringList(CompanyIntroList);
}
public
EstateInfoJson(Parcel in)
{
CompanyIntroList =
new
ArrayList<String>();
in.readStringList(CompanyIntroList);
}
|
2.带有其他类的属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
EstateParamJson EstateParam;
@Override
public
void
writeToParcel(Parcel dest,
int
flags)
{
dest.writeValue(EstateParam);
}
@Override
public
EstateInfoJson(Parcel in)
{
EstateParam = (EstateParamJson) in.readValue(EstateParamJson.
class
.getClassLoader());
}
|
3.带有其他类的数组类型的属性
1
2
3
4
5
6
7
8
9
10
11
|
public
ArrayList<PicJson> shiJing;
//实景图
@Override
public
void
writeToParcel(Parcel dest,
int
flags)
{
dest.writeTypedList(shiJing);
}
public
GalleryJson(Parcel in)
{
shiJing =
new
ArrayList<PicJson>();
in.readTypedList(shiJing, PicJson.CREATOR);
}
|
4.带有子类父类的情况:
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
39
40
41
42
43
44
45
46
47
48
|
public
abstract
class
A
implements
Parcelable {
private
int
a;
protected
A(
int
a) {
this
.a = a;
}
public
void
writeToParcel(Parcel out,
int
flags) {
out.writeInt(a);
}
protected
A(Parcel in) {
a = in.readInt();
}
}
public
class
B
extends
A {
private
int
b;
public
B(
int
a,
int
b) {
super
(a);
this
.b = b;
}
public
static
final
Parcelable.Creator<B> CREATOR =
new
Parcelable.Creator<B>() {
public
B createFromParcel(Parcel in) {
return
new
B(in);
}
public
B[] newArray(
int
size) {
return
new
B[size];
}
};
public
int
describeContents() {
return
0
;
}
public
void
writeToParcel(Parcel out,
int
flags) {
super
.writeToParcel(out, flags);
out.writeInt(b);
}
private
B(Parcel in) {
super
(in);
b = in.readInt();
}
}
|
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1217554,如需转载请自行联系原作者