程序主界面:
点击list按钮传递数据:
点击parcelable传递数据:
点击serializable传递数据:
下面看代码:
一、MainAcitivty.java类的代码:
001 | package com.cn.daming; |
005 | import java.io.Serializable; |
007 | import java.util.ArrayList; |
009 | import java.util.HashMap; |
011 | import java.util.List; |
017 | import android.app.Activity; |
019 | import android.app.ListActivity; |
021 | import android.content.Intent; |
023 | import android.graphics.Color; |
025 | import android.graphics.drawable.GradientDrawable; |
027 | import android.graphics.drawable.GradientDrawable.Orientation; |
029 | import android.os.Bundle; |
031 | import android.view.View; |
033 | import android.view.View.OnClickListener; |
035 | import android.widget.Button; |
039 | public class MainActivity extends Activity implements Serializable { |
043 | private static final long serialVersionUID = 1L; |
047 | private String s_name; |
055 | private Button list_Button; |
057 | private Button ser_Button; |
059 | private Button par_Button; |
061 | private ArrayList<string> m_list; |
065 | public final static String PAR_KEY = "com.cn.daming.parcelable" ; |
067 | public final static String SER_KEY = "com.cn.daming.serializable" ; |
069 | public final static String LIST_KEY = "com.cn.daming.ArrayList" ; |
075 | public void onCreate(Bundle savedInstanceState) { |
077 | super .onCreate(savedInstanceState); |
079 | setContentView(R.layout.main); |
095 | public void initlist() { |
097 | m_list = new ArrayList<string>(); |
099 | m_list.add( "大明ArrayList" ); |
101 | m_list.add( "年龄:25岁" ); |
109 | public void drawBackground() { |
111 | GradientDrawable grad = new GradientDrawable(Orientation.TL_BR, |
113 | new int [] { Color.rgb( 0 , 0 , 127 ), Color.rgb( 0 , 0 , 255 ), |
115 | Color.rgb( 127 , 0 , 255 ), Color.rgb( 127 , 127 , 255 ), |
117 | Color.rgb( 127 , 255 , 255 ), Color.rgb( 255 , 255 , 255 ) }); |
121 | this .getWindow().setBackgroundDrawable(grad); |
127 | public void initList_Button() { |
129 | list_Button = (Button) findViewById(R.id.list_button); |
131 | list_Button.setOnClickListener( new OnClickListener() { |
137 | public void onClick(View arg0) { |
139 | Intent list_intent = new Intent(); |
141 | list_intent.putStringArrayListExtra(LIST_KEY, m_list); |
143 | list_intent.setClass(MainActivity. this , ShowListView. class ); |
145 | startActivity(list_intent); |
155 | public void initPar_Button() { |
157 | par_Button = (Button) findViewById(R.id.par_button); |
159 | par_Button.setOnClickListener( new OnClickListener() { |
165 | public void onClick(View arg0) { |
167 | Student m_Student = new Student(); |
169 | m_Student.setName( "大明例子" ); |
171 | m_Student.setAge( 25 ); |
173 | m_Student.setSex( "男" ); |
175 | Intent p_Intent = new Intent(MainActivity. this , |
179 | Bundle mBundle = new Bundle(); |
181 | mBundle.putParcelable(PAR_KEY, m_Student); |
183 | p_Intent.putExtras(mBundle); |
185 | startActivity(p_Intent); |
195 | public void inintSer_Button() { |
197 | ser_Button = (Button) findViewById(R.id.ser_button); |
199 | ser_Button.setOnClickListener( new OnClickListener() { |
205 | public void onClick(View arg0) { |
207 | MainActivity s_activity = new MainActivity(); |
209 | s_activity.setS_name( "Daming Serlizable!" ); |
211 | s_activity.setS_number( 25 ); |
213 | s_activity.setS_sex( "男" ); |
215 | Intent mIntent = new Intent(MainActivity. this , |
219 | Bundle mBundle = new Bundle(); |
221 | mBundle.putInt( "state" , 3 ); |
223 | mBundle.putSerializable(SER_KEY, s_activity); |
225 | mIntent.putExtras(mBundle); |
227 | startActivity(mIntent); |
237 | public void setS_name(String s_name) { |
239 | this .s_name = s_name; |
245 | public String getS_name() { |
253 | public void setS_number( int s_number) { |
255 | this .s_number = s_number; |
261 | public int getS_number() { |
269 | public void setS_sex(String s_sex) { |
277 | public String getS_sex() { |
二、Student.java类的代码:
001 | package com.cn.daming; |
005 | import android.os.Parcel; |
007 | import android.os.Parcelable; |
011 | public class Student implements Parcelable { |
023 | public String getName() { |
031 | public void setName(String name) { |
047 | public void setAge( int age) { |
055 | public String getSex() { |
063 | public void setSex(String sex) { |
071 | public static final Parcelable.Creator<student> CREATOR = new Creator<student>() { |
073 | public Student createFromParcel(Parcel source) { |
075 | Student mStudent = new Student(); |
077 | mStudent.name = source.readString(); |
079 | mStudent.age = source.readInt(); |
081 | mStudent.sex = source.readString(); |
089 | public Student[] newArray( int size) { |
091 | return new Student[size]; |
101 | public int describeContents() { |
113 | public void writeToParcel(Parcel parcel, int arg1) { |
115 | parcel.writeString(name); |
117 | parcel.writeInt(age); |
119 | parcel.writeString(sex); |
三、ShowListView.java类的代码:
05 | import java.util.ArrayList; |
09 | import android.app.Activity; |
11 | import android.content.Intent; |
13 | import android.graphics.Color; |
15 | import android.graphics.drawable.GradientDrawable; |
17 | import android.graphics.drawable.GradientDrawable.Orientation; |
19 | import android.os.Bundle; |
21 | import android.widget.TextView; |
25 | public class ShowListView extends Activity { |
29 | private Intent list_intent; |
31 | private ArrayList<string> m_arrayList; |
33 | private TextView list_textview; |
39 | protected void onCreate(Bundle savedInstanceState) { |
41 | super .onCreate(savedInstanceState); |
43 | setContentView(R.layout.show_list_view); |
47 | list_textview = (TextView) findViewById(R.id.list_text_view); |
49 | list_intent = getIntent(); |
51 | m_arrayList = list_intent.getExtras().getStringArrayList( |
53 | MainActivity.LIST_KEY); |
57 | list_textview.setText(m_arrayList.get( 0 ) + " \n" + m_arrayList.get( 1 ) |
59 | + "\n" + m_arrayList.get( 2 )); |
65 | public void drawBackground() { |
67 | GradientDrawable grad = new GradientDrawable(Orientation.TL_BR, |
69 | new int [] { Color.rgb( 0 , 0 , 127 ), Color.rgb( 0 , 0 , 255 ), |
71 | Color.rgb( 127 , 0 , 255 ), Color.rgb( 127 , 127 , 255 ), |
73 | Color.rgb( 127 , 255 , 255 ), Color.rgb( 255 , 255 , 255 ) }); |
77 | this .getWindow().setBackgroundDrawable(grad); |
四、ShowParView.java类的代码:
05 | import android.app.Activity; |
07 | import android.graphics.Color; |
09 | import android.graphics.drawable.GradientDrawable; |
11 | import android.graphics.drawable.GradientDrawable.Orientation; |
13 | import android.os.Bundle; |
15 | import android.widget.TextView; |
19 | public class ShowParView extends Activity { |
23 | private TextView par_text_view; |
29 | protected void onCreate(Bundle savedInstanceState) { |
31 | super .onCreate(savedInstanceState); |
33 | setContentView(R.layout.show_par_view); |
39 | par_text_view = (TextView) findViewById(R.id.par_text_view); |
41 | Student p_student = (Student) getIntent().getParcelableExtra( |
43 | MainActivity.PAR_KEY); |
45 | par_text_view.setText( "姓名: " + p_student.getName() + "\n" + "年龄: " |
47 | + p_student.getAge() + "\n" + "性别 : " + p_student.getSex() |
49 | + "\n" + "类:" + p_student.getClass()); |
57 | public void drawBackground() { |
59 | GradientDrawable grad = new GradientDrawable(Orientation.TL_BR, |
61 | new int [] { Color.rgb( 0 , 0 , 127 ), Color.rgb( 0 , 0 , 255 ), |
63 | Color.rgb( 127 , 0 , 255 ), Color.rgb( 127 , 127 , 255 ), |
65 | Color.rgb( 127 , 255 , 255 ), Color.rgb( 255 , 255 , 255 ) }); |
69 | this .getWindow().setBackgroundDrawable(grad); |
五、ShowSerView.java类的代码:
05 | import android.app.Activity; |
07 | import android.graphics.Color; |
09 | import android.graphics.drawable.GradientDrawable; |
11 | import android.graphics.drawable.GradientDrawable.Orientation; |
13 | import android.os.Bundle; |
15 | import android.widget.TextView; |
19 | public class ShowSerView extends Activity { |
21 | private TextView ser_text_view; |
27 | protected void onCreate(Bundle savedInstanceState) { |
29 | super .onCreate(savedInstanceState); |
31 | setContentView(R.layout.show_ser_view); |
37 | ser_text_view = (TextView) findViewById(R.id.ser_text_view); |
39 | MainActivity s_activity = (MainActivity) getIntent() |
41 | .getSerializableExtra(MainActivity.SER_KEY); |
43 | ser_text_view.setText( "You name is: " + s_activity.getS_name() + "\n" |
45 | + "You age is: " + s_activity.getS_number() + "\n" |
47 | + "You sex is: " + s_activity.getS_sex()); |
53 | public void drawBackground() { |
55 | GradientDrawable grad = new GradientDrawable(Orientation.TL_BR, |
57 | new int [] { Color.rgb( 0 , 0 , 127 ), Color.rgb( 0 , 0 , 255 ), |
59 | Color.rgb( 127 , 0 , 255 ), Color.rgb( 127 , 127 , 255 ), |
61 | Color.rgb( 127 , 255 , 255 ), Color.rgb( 255 , 255 , 255 ) }); |
65 | this .getWindow().setBackgroundDrawable(grad); |
xml布局文件
一、main.xml布局文件:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
05 | android:orientation = "vertical" |
07 | android:layout_width = "fill_parent" |
09 | android:layout_height = "fill_parent" |
15 | android:layout_width = "fill_parent" |
17 | android:layout_height = "wrap_content" |
19 | android:layout_gravity = "center_horizontal" |
21 | android:gravity = "center_horizontal" |
23 | android:text = "@string/hello" |
25 | android:textSize = "12pt" |
31 | android:id = "@+id/list_button" |
33 | android:layout_width = "fill_parent" |
35 | android:layout_height = "wrap_content" |
37 | android:layout_gravity = "center_horizontal" |
39 | android:gravity = "center_horizontal" |
41 | android:layout_marginTop = "10dip" |
43 | android:text = "Intent传递list" |
45 | android:textSize = "12pt" |
51 | android:id = "@+id/par_button" |
53 | android:layout_width = "fill_parent" |
55 | android:layout_height = "wrap_content" |
57 | android:layout_marginTop = "10dip" |
59 | android:layout_gravity = "center" |
61 | android:text = "parcelable传递对象" |
63 | android:textSize = "12pt" |
69 | android:id = "@+id/ser_button" |
71 | android:layout_width = "fill_parent" |
73 | android:layout_height = "wrap_content" |
75 | android:layout_gravity = "center_horizontal" |
77 | android:gravity = "center_horizontal" |
79 | android:layout_marginTop = "10dip" |
81 | android:text = "serializable传递对象" |
83 | android:textSize = "12pt" |
二、show_list_view.xml布局文件:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
05 | android:orientation = "vertical" |
07 | android:layout_width = "fill_parent" |
09 | android:layout_height = "fill_parent" |
15 | android:layout_width = "fill_parent" |
17 | android:layout_height = "wrap_content" |
19 | android:layout_marginBottom = "5dip" |
21 | android:layout_gravity = "center_horizontal" |
23 | android:gravity = "center_horizontal" |
25 | android:text = "传递过来的ArrayList的值" |
27 | android:textSize = "12pt" |
33 | android:id = "@+id/list_text_view" |
35 | android:layout_width = "fill_parent" |
37 | android:layout_height = "wrap_content" |
39 | android:layout_marginBottom = "10dip" |
41 | android:layout_gravity = "center_horizontal" |
43 | android:gravity = "center_horizontal" |
45 | android:textSize = "12pt" |
三、show_par_view.xml布局文件:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
05 | android:orientation = "vertical" |
07 | android:layout_width = "fill_parent" |
09 | android:layout_height = "fill_parent" |
15 | android:layout_width = "fill_parent" |
17 | android:layout_height = "wrap_content" |
19 | android:layout_gravity = "center_horizontal" |
21 | android:layout_marginBottom = "10dip" |
23 | android:gravity = "center_horizontal" |
25 | android:text = "接受从MainActivity中传递过来的对象" |
27 | android:textSize = "12pt" |
33 | android:layout_width = "fill_parent" |
35 | android:layout_height = "wrap_content" |
37 | android:layout_marginBottom = "5dip" |
39 | android:layout_gravity = "center_horizontal" |
41 | android:gravity = "center_horizontal" |
43 | android:text = "传递过来的Parcelable值" |
45 | android:textSize = "12pt" |
51 | android:id = "@+id/par_text_view" |
53 | android:layout_width = "fill_parent" |
55 | android:layout_height = "wrap_content" |
57 | android:layout_gravity = "center_horizontal" |
59 | android:gravity = "center_horizontal" |
61 | android:textSize = "12pt" |
四、show_ser_view.xml布局文件:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
05 | android:orientation = "vertical" |
07 | android:layout_width = "fill_parent" |
09 | android:layout_height = "fill_parent" |
15 | android:layout_width = "fill_parent" |
17 | android:layout_height = "wrap_content" |
19 | android:layout_marginBottom = "5dip" |
21 | android:layout_gravity = "center_horizontal" |
23 | android:gravity = "center_horizontal" |
25 | android:text = "传递过来的Serializable值" |
27 | android:textSize = "12pt" |
33 | android:id = "@+id/ser_text_view" |
35 | android:layout_width = "fill_parent" |
37 | android:layout_height = "wrap_content" |
39 | android:layout_marginBottom = "10dip" |
41 | android:layout_gravity = "center_horizontal" |
43 | android:gravity = "center_horizontal" |
45 | android:textSize = "12pt" |
Manifest.xml布局文件:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
05 | package = "com.cn.daming" android:versionCode = "1" android:versionName = "1.0" > |
07 | < application android:icon = "@drawable/icon" android:label = "@string/app_name" > |
09 | < activity android:name = ".MainActivity" android:label = "@string/app_name" > |
13 | < action android:name = "android.intent.action.MAIN" /> |
15 | < category android:name = "android.intent.category.LAUNCHER" /> |
21 | < activity android:name = ".ShowListView" /> |
23 | < activity android:name = ".ShowParView" /> |
25 | < activity android:name = ".ShowSerView" /> |
29 | < uses-sdk android:minSdkVersion = "8" /> |
android 中
自定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable。
一 序列化原因:
1.永久性保存对象,保存对象的字节序列到本地文件中;
2.通过序列化对象在网络中传递对象;
3.通过序列化在进程间传递对象。
二 至于选取哪种可参考下面的原则:
1.在使用内存的时候,Parcelable 类比Serializable性能高,所以推荐使用Parcelable类。
2.Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3.Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点, 也不提倡用,但在这种情况下,还是建议你用Serializable 。
实现:
1 Serializable 的实现,只需要继承 implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。
2 Parcelabel 的实现,需要在类中添加一个静态成员变量 CREATOR,这个变量需要继承 Parcelable.Creator 接口。