MainActivity如下:
package cc.test.serializable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.content.Intent; /** * Demo描述: * 利用Serializable在Activity之间传递自定义对象 * * 步骤: * 1 JavaBean数据实现Serializable接口 * 2 通过intent.putExtra(name, value)携带数据 * 3 通过intent.getSerializableExtra(key)获取传递过来的数据 */ public class MainActivity extends Activity { private Button mButton; private final String key="testSerializable"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Student student=new Student(7, "zxx"); Intent intent=new Intent(MainActivity.this, AnotherActivity.class); intent.putExtra(key, student); startActivity(intent); } }); } }
AnotherActivity如下:
package cc.test.serializable; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class AnotherActivity extends Activity { private TextView mTextView; private final String key="testSerializable"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.another); init(); } private void init() { mTextView=(TextView) findViewById(R.id.textView); //通过getSerializableExtra()方法获取Serializable数据 Student student=(Student) this.getIntent().getSerializableExtra(key); mTextView.setText("Serializable传递过来的数据为:\n"+"id="+student.getId()+" name="+student.getName()); } }
Student如下:
package cc.test.serializable; import java.io.Serializable; public class Student implements Serializable { private Integer id; private String name; //注意定义此字段 public static final long serialVersionUID = 9527L; public Student() { super(); } public Student(Integer id, String name) { super(); this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Test Serializable" android:layout_centerInParent="true" /> </RelativeLayout>
another.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>