發這篇部落客要講一下Android中Intent中如何傳值的幾種方法:
1:基本資料類型,包含了Java八種基本資料類型和CharSequece文本
2:八種資料類新對應數組和CharSequece文本數組
3:Bundle傳值
4:ArrayList集合
5:Serializable傳遞對象
6:Parcelable傳遞對象
在 main.xml 布局檔案中添加六個Button控件,分别是六種傳值方式。
Activity代碼如下:
1 package com.example.transmittingdata;
2
3 import java.util.ArrayList;
4
5 import android.app.Activity;
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.view.View;
9 import android.view.View.OnClickListener;
10
11 /***
12 *
13 * Intent傳值包含
14 * 1:基本資料類型,包含了Java八種基本資料類型和CharSequece文本
15 * 2:八種資料類新對應數組和CharSequece文本數組
16 * 3:Bundle
17 * 4:ArrayList集合 5:Serializable傳遞對象
18 * 6:Parcelable傳遞對象
19 *
20 * @author zq
21 *
22 */
23 public class MainActivity extends Activity implements OnClickListener {
24
25 private String[] str = new String[] { "八種資料類新對應數組和CharSequece文本數組", "123" };
26 private ArrayList<String> list;
27
28 @Override
29 protected void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.main);
32 initView();
33 }
34
35 private void initView() {
36 // TODO Auto-generated method stub
37 findViewById(R.id.button1).setOnClickListener(this);
38 findViewById(R.id.button2).setOnClickListener(this);
39 findViewById(R.id.button3).setOnClickListener(this);
40 findViewById(R.id.button4).setOnClickListener(this);
41 findViewById(R.id.button5).setOnClickListener(this);
42 findViewById(R.id.button6).setOnClickListener(this);
43 list = new ArrayList<String>();
44 list.add("List集合");
45 list.add("Value");
46
47 }
48
49 @Override
50 public void onClick(View v) {
51 // TODO Auto-generated method stub
52 Intent intent = new Intent(MainActivity.this, IntentData.class);
53 switch (v.getId()) {
54 case R.id.button1:
55 intent.putExtra("i", 1);
56 intent.putExtra("key", "基本資料類型,包含了Java八種基本資料類型和CharSequece文本");
57 break;
58 case R.id.button2:
59 intent.putExtra("i", 2);
60 intent.putExtra("key", str);
61 break;
62 case R.id.button3:
63 Bundle bundle = new Bundle();
64 bundle.putString("key", "Bundle傳值");
65 intent.putExtra("i", 3);
66 intent.putExtra("bundle", bundle);
67 break;
68 case R.id.button4:
69 intent.putExtra("i", 4);
70 intent.putStringArrayListExtra("key", list);
71 break;
72 case R.id.button5:
73 UserInfo user = new UserInfo();
74 user.setSex("男");
75 user.setUserName("白子畫");
76 intent.putExtra("i", 5);
77 intent.putExtra("key", user);
78 break;
79
80 case R.id.button6:
81 intent.putExtra("i", 6);
82 UserBean userBean = new UserBean();
83 userBean.setSex("女");
84 userBean.setUserName("花千骨");
85 intent.putExtra("key", userBean);
86 break;
87
88 default:
89 break;
90 }
91 startActivity(intent);
92 }
93
94 }
接收值的Activity類:
1 public class IntentData extends Activity {
2
3 private TextView text1, text2;
4 private int position = 1;
5 private String data = "";
6
7 @Override
8 protected void onCreate(Bundle savedInstanceState) {
9 // TODO Auto-generated method stub
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.data);
12 initView();
13 initData();
14
15 }
16
17 @SuppressLint("NewApi")
18 private void initData() {
19 // TODO Auto-generated method stub
20 if (getIntent() != null) {
21 position = getIntent().getIntExtra("i", 1);
22 if (position == 1) {
23 data = getIntent().getStringExtra("key");
24 text1.setText("基本資料類型");
25 text2.setText(data);
26 return;
27 }
28 if (position == 2) {
29 String[] data1 = getIntent().getStringArrayExtra("key");
30 text1.setText("數組");
31 text2.setText(data1[0] + "----" + data1[1]);
32 return;
33 }
34 if (position == 3) {
35 Bundle bundle = getIntent().getBundleExtra("bundle");
36 text1.setText("Bundle");
37 text2.setText(bundle.getString("key", "預設為空是的值"));
38 return;
39 }
40 if (position == 4) {
41 ArrayList<String> array;
42 text1.setText("List<object> 集合");
43 array = getIntent().getStringArrayListExtra("key");
44 text2.setText(array.get(0));
45 return;
46 }
47 if (position == 5) {
48 UserInfo user;
49 text1.setText("Serializable傳遞對象");
50 user = (UserInfo) getIntent().getSerializableExtra("key");
51 text2.setText(user.getUserName() + "---" + user.getSex());
52 return;
53 }
54 if (position == 6) {
55 UserBean userBean;
56 text1.setText("Parcelable傳遞對象");
57 userBean = (UserBean) getIntent().getParcelableExtra("key");
58 text2.setText(userBean.getUserName() + "---"
59 + userBean.getSex());
60 return;
61 }
62
63 }
64 }
65
66 private void initView() {
67 // TODO Auto-generated method stub
68 text1 = (TextView) findViewById(R.id.textView1);
69 text2 = (TextView) findViewById(R.id.textView2);
70
71 }
72
73 }
Serializable傳遞對象
Serializable:是序列化的意思,表示将一個對象轉換成可儲存或可傳輸的狀态,對象進行Serializable序列化之後就可以通過Intent來進行Activity之間的傳輸了。
1 public class UserInfo implements Serializable{
2 /**
3 *
4 */
5 private static final long serialVersionUID = 1L;
6 private String sex;
7 private String userName;
8
9 public UserInfo() {
10 // TODO Auto-generated constructor stub
11 }
12
13 public String getSex() {
14 return sex;
15 }
16 public void setSex(String sex) {
17 this.sex = sex;
18 }
19 public String getUserName() {
20 return userName;
21 }
22 public void setUserName(String userName) {
23 this.userName = userName;
24 }
25
26
27 }
這裡面的serialVersionUID需要注意一下,它的作用是序列化和反序列化時保持版本的相容性,如果你未指定,運作時也會預設生成,在進行反序列化時隻有資料和目前類的serialVersionUID相同是才能夠正常的反序列化,你不指定serialVersionUID一般情況下也不會出問題,但是如果目前類發生了改變例如删掉了某個成員變量那麼目前類的serialVersionUID也會出現改變,之後你對資料進行反序列化就會出現錯誤,這裡我指定為1L,L為Long資料類型。
Parcelable傳遞對象
Parcelable的序列化原理是将一個對象進行分解,而分解後的每一部分都是Intent所支援的資料類型,是以實作了傳遞對象的功能。
1 public class UserBean implements Parcelable {
2
3 private String sex;
4 private String userName;
5
6 public UserBean() {
7 // TODO Auto-generated constructor stub
8 }
9
10 public String getSex() {
11 return sex;
12 }
13
14 public void setSex(String sex) {
15 this.sex = sex;
16 }
17
18 public String getUserName() {
19 return userName;
20 }
21
22 public void setUserName(String userName) {
23 this.userName = userName;
24 }
25
26 @Override
27 public int describeContents() {
28 // TODO Auto-generated method stub
29 return 0;
30 }
31
32 @Override
33 public void writeToParcel(Parcel dest, int flags) {
34 // TODO Auto-generated method stub
35
36 dest.writeString(userName);
37 dest.writeString(sex);
38 }
39
40 protected UserBean(Parcel in) {
41 userName = in.readString();
42 sex = in.readString();
43 }
44
45 public static final Creator<UserBean> CREATOR = new Creator<UserBean>() {
46
47 @Override
48 public UserBean createFromParcel(Parcel in) {
49 return new UserBean(in);
50 }
51
52 @Override
53 public UserBean[] newArray(int size) {
54 return new UserBean[size];
55 }
56
57 };
可以看到通過Parcelable的實作方式是要複雜很多的,實作Parcelable接口後,需要重寫writeToParcel和describeContents方法,describeContents方法直接傳回0就可以了,writeToParcel方法我們需要調用Parcel對象進行資料寫入,例如dest.writeString(name),注意如果name是字元串類型就調用writeString,如果是Int類型就調用writeInt 等等。
源碼下載下傳: https://github.com/DickyQie/android-data-type/