Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。
要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。
Intent中传递这2种对象的方法:
- Bundle.putSerializable(Key,Object); //实现Serializable接口的对象
- Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象
一、传递对象
假设由登录界面(Login)跳转到主界面(MainActivity)传递的对象为登录的用户信息 User类
1.首先创建一个序列化类:User
- package org.yayun.demo;
- import java.io.Serializable;
- import android.R.integer;
- public class User implements Serializable {
- private String name;
- private int level;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getLevel() {
- return level;
- }
- public void setLevel(int level) {
- this.level = level;
- }
- public User(String name,int level) {
- this.name=name;
- this.level=level;
- }
- }
注意点,类实现Serializable接口
2.布局文件很简单,main.xml中一个Button,login.xml中一个TextView:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="登录" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="" />
- </LinearLayout>
3.MainActivity.java
- package org.yayun.demo;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button button;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState); // 生命周期方法
- super.setContentView(R.layout.main); // 设置要使用的布局管理器
- button=(Button)findViewById(R.id.btn);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- User user=new User("yayun", 7);//实例化对象
- Intent intent=new Intent();
- intent.setClass(MainActivity.this, LoginActivity.class);
- Bundle bundle=new Bundle();
- bundle.putSerializable("user", user);//序列化
- intent.putExtras(bundle);//发送数据
- startActivity(intent);//启动intent
- }
- });
- }
- }
4.接收Activity--LoginActivity.java:
- package org.yayun.demo;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- public class LoginActivity extends Activity {
- private TextView textView;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState); // 生命周期方法
- super.setContentView(R.layout.login); // 设置要使用的布局管理器
- textView = (TextView) findViewById(R.id.text);
- Intent intent = this.getIntent();
- User user = (User) intent.getSerializableExtra("user");
- textView.setText("用户名:" + user.getName() + "用户等级"
- + String.valueOf(user.getLevel()));
- }
- }
5.运行实例如下:
可以看出,对象已经成功传过来了!
二、传递对象集合
1.布局文件没有改变,我们看一下MainActivity.java:
- package org.yayun.demo;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button button;
- List<User> list=new ArrayList<User>();
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState); // 生命周期方法
- super.setContentView(R.layout.main); // 设置要使用的布局管理器
- button=(Button)findViewById(R.id.btn);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- User user1=new User("yayun", 7);//实例化对象
- User user2=new User("feifei", 9);
- list.add(user1);
- list.add(user2);
- Intent intent=new Intent();
- intent.setClass(MainActivity.this, LoginActivity.class);
- Bundle bundle=new Bundle();
- bundle.putSerializable("list",(Serializable)list);//序列化,要注意转化(Serializable)
- intent.putExtras(bundle);//发送数据
- startActivity(intent);//启动intent
- }
- });
- }
- }
2.看一下接收Activity--LoginActivity.java
- package org.yayun.demo;
- import java.util.List;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- public class LoginActivity extends Activity {
- private TextView textView;
- List<User> list;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState); // 生命周期方法
- super.setContentView(R.layout.login); // 设置要使用的布局管理器
- textView = (TextView) findViewById(R.id.text);
- Intent intent = this.getIntent();
- list = (List<User>) intent.getSerializableExtra("list");//获取list方式
- User user1=list.get(0);
- User user2=list.get(1);
- textView.setText("用户名:" + user2.getName() + "用户等级"
- + String.valueOf(user2.getLevel()));
- }
- }
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方式
喜欢的朋友点个赞,关注一下!谢谢