Activity间传类对象数据

本文介绍在Android开发中如何在Activity之间传递复杂对象,包括单个对象和List对象的序列化与 Parcelable 方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://blog.youkuaiyun.com/lincyang/article/details/7080676

Activity间要传递数据,很多时候简单的int和string满足不了需求,我们在面向对象中已经习惯了类和对象,

那么下面就说一下如何传递类对象。

一、单个对象

        Android中有两种办法来完成这件事。一个是Java的序列化(Serializable),另一个是Android的Parcelable序列化方法。

对于第一种办法实现很简单,只需要实现Serializable接口就好了。就像下面是一个person类:

  1. package com.linc.meta;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. @SuppressWarnings("serial")  
  6. public class Person2 implements Serializable {  
  7.     private String Name = null;  
  8.     private String Gender = null;  
  9.     private int HP = 0;  
  10.     private String Summary = null;  
  11.     private String Skill = null;  
  12.       
  13.     public Person2(String name,String gender,int HP,String summary,String skill)  
  14.     {  
  15.         this.Name = name;  
  16.         this.Gender = gender;  
  17.         this.Summary = summary;  
  18.         this.HP = HP;  
  19.         this.Skill = skill;  
  20.     }  
  21.       
  22.     public String getName() {  
  23.         return Name;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         Name = name;  
  28.     }  
  29.   
  30.     public String getGender() {  
  31.         return Gender;  
  32.     }  
  33.   
  34.     public void setGender(String gender) {  
  35.         Gender = gender;  
  36.     }  
  37.   
  38.     public int getHP() {  
  39.         return HP;  
  40.     }  
  41.   
  42.     public void setHP(int hP) {  
  43.         HP = hP;  
  44.     }  
  45.   
  46.     public String getSummary() {  
  47.         return Summary;  
  48.     }  
  49.   
  50.     public void setSummary(String summary) {  
  51.         Summary = summary;  
  52.     }  
  53.   
  54.       
  55.       
  56.     public String getSkill() {  
  57.         return Skill;  
  58.     }  
  59.   
  60.     public void setSkill(String skill) {  
  61.         Skill = skill;  
  62.     }  
  63. }  

怎么样,很简单的把原来的person类实现Serializable接口就可以了。

对于第二种办法,实现起来还是稍微有点小磕绊。

这是Android新提供的序列化类,首先要看看Parcel类 ,它是一个数据的容器,只要你的类实现Parcelable接口,并完成几个步骤就可以放到Parcel中传递了。

有三个步骤要完成:

一、完成writeToParcel(Parcel dest, int flags)方法:该方法将类的数据写入外部提供的Parcel中

二、完成describeContents():这是返回描述性的东西,返回值是整型,通常是0.如果知道marshalled,请看CONTENTS_FILE_DESCRIPTOR

三、Parcelable.Creator 接口

        请看下面,还是person类,它实现了Parcelable接口。

  1. package com.linc.meta;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. public class Person implements Parcelable {  
  7.     private String Name = null;  
  8.     private String Gender = null;  
  9.     private int HP = 0;  
  10.     private String Summary = null;  
  11.     private String Skill = null;  
  12.       
  13.     public Person(String name,String gender,int HP,String summary,String skill)  
  14.     {  
  15.         this.Name = name;  
  16.         this.Gender = gender;  
  17.         this.Summary = summary;  
  18.         this.HP = HP;  
  19.         this.Skill = skill;  
  20.     }  
  21.       
  22.     public String getName() {  
  23.         return Name;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         Name = name;  
  28.     }  
  29.   
  30.     public String getGender() {  
  31.         return Gender;  
  32.     }  
  33.   
  34.     public void setGender(String gender) {  
  35.         Gender = gender;  
  36.     }  
  37.   
  38.     public int getHP() {  
  39.         return HP;  
  40.     }  
  41.   
  42.     public void setHP(int hP) {  
  43.         HP = hP;  
  44.     }  
  45.   
  46.     public String getSummary() {  
  47.         return Summary;  
  48.     }  
  49.   
  50.     public void setSummary(String summary) {  
  51.         Summary = summary;  
  52.     }  
  53.   
  54.       
  55.       
  56.     public String getSkill() {  
  57.         return Skill;  
  58.     }  
  59.   
  60.     public void setSkill(String skill) {  
  61.         Skill = skill;  
  62.     }  
  63.     //Describe the kinds of special objects contained in this Parcelable's marshalled representation.  
  64.     //CONTENTS_FILE_DESCRIPTOR  
  65.     @Override  
  66.     public int describeContents() {  
  67.         // TODO Auto-generated method stub  
  68.         return 0;  
  69.     }  
  70.       
  71.     //该方法将类的数据写入外部提供的Parcel中  
  72.     @Override  
  73.     public void writeToParcel(Parcel dest, int flags) {  
  74.         dest.writeString(Name);  
  75.         dest.writeString(Gender);  
  76.         dest.writeString(Summary);  
  77.         dest.writeInt(HP);  
  78.         dest.writeString(Skill);  
  79.     }  
  80.       
  81.     public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>()  
  82.     {  
  83.   
  84.         @Override  
  85.         public Person createFromParcel(Parcel source) {  
  86.             return new Person(source);  
  87.         }  
  88.   
  89.         @Override  
  90.         public Person[] newArray(int size) {  
  91.             return new Person[size];  
  92.         }  
  93.           
  94.     };  
  95.       
  96.     private Person(Parcel dest)  
  97.     {  
  98.         this.Name = dest.readString();  
  99.         this.Gender = dest.readString();  
  100.         this.Summary = dest.readString();  
  101.         this.HP = dest.readInt();  
  102.         this.Skill = dest.readString();  
  103.     }  
  104.           
  105. }  

最后举一个例子,有两个Activity A和B,A中点击button后用Serializable传给B person,然后在B中点击button,把person用parcelable再传回来。

activity A

  1. package com.linc;  
  2.   
  3. import com.linc.meta.Person;  
  4. import com.linc.meta.Person2;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.TextView;  
  13.   
  14. public class ActivityA extends Activity {  
  15.     private TextView txt ;  
  16.     private Button btn;  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         txt = (TextView)findViewById(R.id.txt);  
  24.         btn = (Button)findViewById(R.id.btn);  
  25.         btn.setOnClickListener(new View.OnClickListener() {  
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 transferData();  
  29.             }  
  30.         });  
  31.           
  32.         Intent intent = getIntent();  
  33.           
  34.         Person person = (Person) intent.getParcelableExtra("person");  
  35.         if(person != null)  
  36.             txt.setText("person :\n " + person.getName() + " \n "  
  37.                     + person.getGender() + " \n "  
  38.                     + person.getSummary() + " \n "  
  39.                     + person.getHP() + " \n "  
  40.                     + person.getSkill());  
  41.     }  
  42.       
  43.     private void transferData()  
  44.     {  
  45.         Person2 person = new Person2("张飞""男"4"五虎上将之一。燕山人士,喜欢喝酒,生性豪放重情义。""咆哮");  
  46.         Intent intent = new Intent();  
  47.         intent.setClass(this, ActivityB.class);  
  48.         intent.putExtra("person2", person);  
  49.         startActivity(intent);  
  50.     }  
  51. }  

ActivityB
  1. package com.linc;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9.   
  10. import com.linc.meta.Person;  
  11. import com.linc.meta.Person2;  
  12.   
  13. public class ActivityB extends Activity {  
  14.     private TextView txt ;  
  15.     private Button btn;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         txt = (TextView)findViewById(R.id.txt);  
  21.         btn = (Button)findViewById(R.id.btn);  
  22.         btn.setOnClickListener(new View.OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 transferData();  
  26.             }  
  27.         });  
  28.           
  29.         Intent intent = getIntent();  
  30.         if(intent != null)  
  31.         {  
  32.             Person2 person = (Person2) intent.getSerializableExtra("person2");  
  33.             txt.setText("person : \n" + person.getName() + " \n "  
  34.                     + person.getGender() + " \n "  
  35.                     + person.getSummary() + " \n "  
  36.                     + person.getHP() + " \n "  
  37.                     + person.getSkill());  
  38.         }  
  39.     }  
  40.       
  41.     private void transferData()  
  42.     {  
  43.         Person person = new Person("黄盖""男"4"东吴老将,配合周瑜使用苦肉计,骗过了将干,舍身无畏。""苦肉");  
  44.         Intent intent = new Intent();  
  45.         intent.setClass(this, ActivityA.class);  
  46.         intent.putExtra("person", person);  
  47.         startActivity(intent);  
  48.     }  
  49. }  

二、List

也可以用Serialize和parcelable来传递多个类对象,这时要强调的是用ArrayList。

就是把多个person放到list中,然后取时用intent.getParcelableArrayListExtra("person_list")取出。就okay了。

序列化也一样,发送正常发,取时类似这样:data = (ArrayList<Person>)getIntent().getSerializableExtra("person_list");



1. 用户与身体信息管理模块 用户信息管理: 注册登录:支持手机号 / 邮箱注册,密码加密存储,提供第三方快捷登录(模拟) 个人资料:记录基本信息(姓名、年龄、性别、身高、体重、职业) 健康目标:用户设置目标(如 “减重 5kg”“增肌”“维持健康”)及期望周期 身体状态跟踪: 体重记录:定期录入体重数据,生成体重变化曲线(折线图) 身体指标:记录 BMI(自动计算)、体脂率(可选)、基础代谢率(根据身高体重估算) 健康状况:用户可填写特殊情况(如糖尿病、过敏食物、素食偏好),系统据此调整推荐 2. 膳食记录与食物数据库模块 食物数据库: 基础信息:包含常见食物(如米饭、鸡蛋、牛肉)的名称、类别(主食 / 肉类 / 蔬菜等)、每份重量 营养成分:记录每 100g 食物的热量(kcal)、蛋白质、脂肪、碳水化合物、维生素、矿物质含量 数据库维护:管理员可添加新食物、更新营养数据,支持按名称 / 类别检索 膳食记录功能: 快速记录:用户选择食物、输入食用量(克 / 份),系统自动计算摄入的营养成分 餐次分类:按早餐 / 午餐 / 晚餐 / 加餐分类记录,支持上餐食照片(可选) 批量操作:提供常见套餐模板(如 “三明治 + 牛奶”),一键添加到记录 历史记录:按日期查看过往膳食记录,支持编辑 / 删除错误记录 3. 营养分析模块 每日营养摄入分析: 核心指标计算:统计当日摄入的总热量、蛋白质 / 脂肪 / 碳水化合物占比(按每日推荐量对比) 微量营养素分析:检查维生素(如维生素 C、钙、铁)的摄入是否达标 平衡评估:生成 “营养平衡度” 评分(0-100 分),指出摄入过剩或不足的营养素 趋势分析: 周 / 月营养趋势:用折线图展示近 7 天 / 30 天的热量、三大营养素摄入变化 对比分析:将实际摄入与推荐量对比(如 “蛋白质摄入仅达到推荐量的 70%”) 目标达成率:针对健
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值