Android Intent之传递带有对象的集合(Serializable传递对象和对象集合)

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

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

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

[java]  view plain  copy
  1. Bundle.putSerializable(Key,Object);  //实现Serializable接口的对象  
  2.   
  3. Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象  

 一、传递对象

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

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

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


注意点,类实现Serializable接口

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

[html]  view plain  copy
  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.   
  7.     <Button  
  8.         android:id="@+id/btn"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="登录" />  
  12.   
  13. </LinearLayout>  
[html]  view plain  copy
  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.   
  7.     <TextView  
  8.         android:id="@+id/text"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="" />  
  12.   
  13. </LinearLayout>  


3.MainActivity.java

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


4.接收Activity--LoginActivity.java:

[java]  view plain  copy
  1. package org.yayun.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. public class LoginActivity extends Activity {  
  9.     private TextView textView;  
  10.   
  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.         User user = (User) intent.getSerializableExtra("user");  
  17.         textView.setText("用户名:" + user.getName() + "用户等级"  
  18.                 + String.valueOf(user.getLevel()));  
  19.   
  20.     }  
  21. }  


5.运行实例如下:

可以看出,对象已经成功传过来了!

二、传递对象集合

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

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


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

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


3.运行实例:

我们看出,已经从对象集合中取出对象了!

总结

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

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

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

4.list = (List<User>) intent.getSerializableExtra("list");//获取对象集合list方式

 

喜欢的朋友点个赞,关注一下!谢谢微笑

猜你喜欢

转载自blog.csdn.net/qq1271396448/article/details/80423128
今日推荐