使用Intent传递对象或集合

原文链接,非常感谢yayun0516的文章

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。

要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。

Intent中传递这2种对象的方法:


     
     
     
     
  1. Bundle.putSerializable(Key,Object); //实现Serializable接口的对象
  2. Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象

 一、传递对象

假设由登录界面(Login)跳转到主界面(MainActivity)传递的对象为登录的用户信息 User类

1.首先创建一个序列化类:User


     
     
     
     
  1. package org.yayun.demo;
  2. import java.io.Serializable;
  3. import android.R.integer;
  4. public class User implements Serializable {
  5. private String name;
  6. private int level;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public int getLevel() {
  14. return level;
  15. }
  16. public void setLevel(int level) {
  17. this.level = level;
  18. }
  19. public User(String name,int level) {
  20. this.name=name;
  21. this.level=level;
  22. }
  23. }


注意点,类实现Serializable接口

2.布局文件很简单,main.xml中一个Button,login.xml中一个TextView:


     
     
     
     
  1. xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "fill_parent"
  4. android:layout_height= "fill_parent"
  5. android:orientation= "vertical" >
  6. <Button
  7. android:id= "@+id/btn"
  8. android:layout_width= "fill_parent"
  9. android:layout_height= "wrap_content"
  10. android:text= "登录" />
  11. LinearLayout>

     
     
     
     
  1. xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "fill_parent"
  4. android:layout_height= "fill_parent"
  5. android:orientation= "vertical" >
  6. <TextView
  7. android:id= "@+id/text"
  8. android:layout_width= "fill_parent"
  9. android:layout_height= "wrap_content"
  10. android:text= "" />
  11. LinearLayout>


3.MainActivity.java


     
     
     
     
  1. package org.yayun.demo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity {
  9. private Button button;
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState); // 生命周期方法
  12. super.setContentView(R.layout.main); // 设置要使用的布局管理器
  13. button=(Button)findViewById(R.id.btn);
  14. button.setOnClickListener( new OnClickListener() {
  15. public void onClick(View v) {
  16. User user= new User( "yayun", 7); //实例化对象
  17. Intent intent= new Intent();
  18. intent.setClass(MainActivity. this, LoginActivity.class);
  19. Bundle bundle= new Bundle();
  20. bundle.putSerializable( "user", user); //序列化
  21. intent.putExtras(bundle); //发送数据
  22. startActivity(intent); //启动intent
  23. }
  24. });
  25. }
  26. }


4.接收Activity–LoginActivity.java:


     
     
     
     
  1. package org.yayun.demo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class LoginActivity extends Activity {
  7. private TextView textView;
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState); // 生命周期方法
  10. super.setContentView(R.layout.login); // 设置要使用的布局管理器
  11. textView = (TextView) findViewById(R.id.text);
  12. Intent intent = this.getIntent();
  13. User user = (User) intent.getSerializableExtra( "user");
  14. textView.setText( "用户名:" + user.getName() + "用户等级"
  15. + String.valueOf(user.getLevel()));
  16. }
  17. }

二、传递对象集合

1.布局文件没有改变,我们看一下MainActivity.java:


     
     
     
     
  1. package org.yayun.demo;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.List;
  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. import android.widget.Button;
  11. public class MainActivity extends Activity {
  12. private Button button;
  13. List list= new ArrayList();
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState); // 生命周期方法
  16. super.setContentView(R.layout.main); // 设置要使用的布局管理器
  17. button=(Button)findViewById(R.id.btn);
  18. button.setOnClickListener( new OnClickListener() {
  19. public void onClick(View v) {
  20. User user1= new User( "yayun", 7); //实例化对象
  21. User user2= new User( "feifei", 9);
  22. list.add(user1);
  23. list.add(user2);
  24. Intent intent= new Intent();
  25. intent.setClass(MainActivity. this, LoginActivity.class);
  26. Bundle bundle= new Bundle();
  27. bundle.putSerializable( "list",(Serializable)list); //序列化,要注意转化(Serializable)
  28. intent.putExtras(bundle); //发送数据
  29. startActivity(intent); //启动intent
  30. }
  31. });
  32. }
  33. }


2.看一下接收Activity–LoginActivity.java


     
     
     
     
  1. package org.yayun.demo;
  2. import java.util.List;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7. public class LoginActivity extends Activity {
  8. private TextView textView;
  9. List list;
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState); // 生命周期方法
  12. super.setContentView(R.layout.login); // 设置要使用的布局管理器
  13. textView = (TextView) findViewById(R.id.text);
  14. Intent intent = this.getIntent();
  15. list = (List) intent.getSerializableExtra( "list"); //获取list方式
  16. User user1=list.get( 0);
  17. User user2=list.get( 1);
  18. textView.setText( "用户名:" + user2.getName() + "用户等级"
  19. + String.valueOf(user2.getLevel()));
  20. }
  21. }


总结

1.Bundle.putSerializable(Key,Object); //实现Serializable接口的对象;

2.获取对象User user = (User) intent.getSerializableExtra(“user”);

3.bundle.putSerializable(“list”,(Serializable)list);//对象集合存入方式;

4.list = (List) intent.getSerializableExtra(“list”);//获取对象集合list方式

使用Parcelable

一、实现对象传递

首先创建User.java实现Parcelable接口:


    
    
    
    
  1. package org.yayun.demo;
  2. import java.io.Serializable;
  3. import android.R.integer;
  4. import android.os.Parcel;
  5. import android.os.Parcelable;
  6. public class User implements Parcelable {
  7. private String name;
  8. private int level;
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public int getLevel() {
  16. return level;
  17. }
  18. public void setLevel(int level) {
  19. this.level = level;
  20. }
  21. public User(String name, int level) {
  22. this.name = name;
  23. this.level = level;
  24. }
  25. public User() { // 构造方法
  26. }
  27. public int describeContents() { // 覆写的方法
  28. return 0;
  29. }
  30. public void writeToParcel(Parcel parcel, int flags) { // 覆写的方法
  31. parcel.writeString(name);
  32. parcel.writeInt(level);
  33. }
  34. public static final Parcelable.Creator CREATOR = new Creator() { //必须实现的方法
  35. public User createFromParcel(Parcel source) {
  36. User user = new User();
  37. user.name = source.readString();
  38. user.level = source.readInt();
  39. return user;
  40. }
  41. public User[] newArray( int size) {
  42. return new User[size];
  43. }
  44. };
  45. }

Parcelable.Creator是必须要实现的,否则会报错:

下面看两个Activity

1.MainActivity.java


    
    
    
    
  1. package org.yayun.demo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity {
  9. private Button button;
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState); // 生命周期方法
  12. super.setContentView(R.layout.main); // 设置要使用的布局管理器
  13. button = (Button) findViewById(R.id.btn);
  14. button.setOnClickListener( new OnClickListener() {
  15. public void onClick(View v) {
  16. User user1 = new User( "yayun", 7); // 实例化对象
  17. Intent intent = new Intent();
  18. intent.setClass(MainActivity. this, LoginActivity.class);
  19. Bundle bundle = new Bundle();
  20. bundle.putParcelable( "user", user1); // 序列化
  21. intent.putExtras(bundle); // 发送数据
  22. startActivity(intent); // 启动intent
  23. }
  24. });
  25. }
  26. }

2.LoginActivity.java:


    
    
    
    
  1. package org.yayun.demo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class LoginActivity extends Activity {
  7. private TextView textView;
  8. User user;
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState); // 生命周期方法
  11. super.setContentView(R.layout.login); // 设置要使用的布局管理器
  12. textView = (TextView) findViewById(R.id.text);
  13. Intent intent = this.getIntent();
  14. user = (User) intent.getParcelableExtra( "user");
  15. textView.setText( "用户名:" + user.getName() + "用户等级"
  16. + String.valueOf(user.getLevel()));
  17. }
  18. }

二、实现对象集合传递

这里我们只需要改动两个Activity

1.MainActivity.java:


    
    
    
    
  1. package org.yayun.demo;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. public class MainActivity extends Activity {
  11. private Button button;
  12. List list= new ArrayList();
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState); // 生命周期方法
  15. super.setContentView(R.layout.main); // 设置要使用的布局管理器
  16. button = (Button) findViewById(R.id.btn);
  17. button.setOnClickListener( new OnClickListener() {
  18. public void onClick(View v) {
  19. User user1 = new User( "yayun", 7); // 实例化对象
  20. User user2= new User( "feifei", 9);
  21. list.add(user1);
  22. list.add(user2);
  23. Intent intent = new Intent();
  24. intent.setClass(MainActivity. this, LoginActivity.class);
  25. Bundle bundle = new Bundle();
  26. bundle.putParcelableArrayList( "list",(ArrayList)list); // 序列化,要注意 intent.putExtras(bundle);// 发送数据
  27. startActivity(intent); // 启动intent
  28. }
  29. });
  30. }
  31. }


2.LoginActivity.java


    
    
    
    
  1. package org.yayun.demo;
  2. import java.util.List;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7. public class LoginActivity extends Activity {
  8. private TextView textView;
  9. List list;
  10. User user2;
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState); // 生命周期方法
  13. super.setContentView(R.layout.login); // 设置要使用的布局管理器
  14. textView = (TextView) findViewById(R.id.text);
  15. Intent intent = this.getIntent();
  16. list = intent.getParcelableArrayListExtra( "list"); //getParcelableArrayListExtra方法
  17. user2=(User)list.get( 1);
  18. textView.setText( "用户名:" + user2.getName() + "用户等级"
  19. + String.valueOf(user2.getLevel()));
  20. }
  21. }


最后注意在AndroidManifest.xml文件中配置LoginActivity

总结

1.实现Parcelable接口注意覆写几个方法和实现Parcelable.Creator方法;

2.bundle.putParcelable(“user”, user1);// 序列化

3.user = (User) intent.getParcelableExtra(“user”);//获取对象

4.bundle.putParcelableArrayList(“list”,(ArrayList)list);//存入List对象数组

5.list = intent.getParcelableArrayListExtra(“list”);//getParcelableArrayListExtra方法获取对象数组

 

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 接口。

你可能感兴趣的:(Android)