Intent传递对象的两种方法(Serializable,Parcelable) (转)
今天讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口
第一步:新建一个Android工程命名为IntentDemo
第二步:修改main.xml布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Welcome to Mr wei's blog."
- />
- <Button
- android:id="@+id/button1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Serializable"
- />
- <Button
- android:id="@+id/button2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Parcelable"
- />
- </LinearLayout>
第三步:新建两个类一个是Person.java实现Serializable接口,另一个Book.java实现Parcelable接口,代码分别如下:
Person.java
- package cn.caiwb.intent;
- import java.io.Serializable;
- public class Person implements Serializable {
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
Book.java
- package cn.caiwb.intent;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class Book implements Parcelable {
- private String bookName;
- private String author;
- private int publishTime;
- public String getBookName() {
- return bookName;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public int getPublishTime() {
- return publishTime;
- }
- public void setPublishTime(int publishTime) {
- this.publishTime = publishTime;
- }
- public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
- public Book createFromParcel(Parcel source) {
- Book mBook = new Book();
- mBook.bookName = source.readString();
- mBook.author = source.readString();
- mBook.publishTime = source.readInt();
- return mBook;
- }
- public Book[] newArray(int size) {
- return new Book[size];
- }
- };
- public int describeContents() {
- return 0;
- }
- public void writeToParcel(Parcel parcel, int flags) {
- parcel.writeString(bookName);
- parcel.writeString(author);
- parcel.writeInt(publishTime);
- }
- }
第四步:修改IntentDemo.java,并且新建两个Activity,一个是IntentDemo1.java,别一个是IntentDemo2.java.分别用来显示Person对像数据,和Book对象数据:,代码分别如下:
IntentDemo.java
- package cn.caiwb.intent;
- 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 IntentDemo extends Activity implements OnClickListener {
- private Button sButton,pButton;
- public final static String SER_KEY = "cn.caiwb.intent.ser";
- public final static String PAR_KEY = "cn.caiwb.intent.par";
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setupViews();
- }
- public void setupViews(){
- sButton = (Button)findViewById(R.id.button1);
- pButton = (Button)findViewById(R.id.button2);
- sButton.setOnClickListener(this);
- pButton.setOnClickListener(this);
- }
- //Serializeable传递对象的方法
- public void SerializeMethod(){
- Person mPerson = new Person();
- mPerson.setName("frankie");
- mPerson.setAge(25);
- Intent mIntent = new Intent(this,IntentDemo1.class);
- Bundle mBundle = new Bundle();
- mBundle.putSerializable(SER_KEY,mPerson);
- mIntent.putExtras(mBundle);
- startActivity(mIntent);
- }
- //Pacelable传递对象方法
- public void PacelableMethod(){
- Book mBook = new Book();
- mBook.setBookName("Android Tutor");
- mBook.setAuthor("Frankie");
- mBook.setPublishTime(2010);
- Intent mIntent = new Intent(this,IntentDemo2.class);
- Bundle mBundle = new Bundle();
- mBundle.putParcelable(PAR_KEY, mBook);
- mIntent.putExtras(mBundle);
- startActivity(mIntent);
- }
- //铵钮点击事件响应
- public void onClick(View v) {
- if(v == sButton){
- SerializeMethod();
- }else{
- PacelableMethod();
- }
- }
- }
IntentDemo1.java
- package cn.caiwb.intent;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class IntentDemo1 extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TextView mTextView = new TextView(this);
- Person mPerson = (Person)getIntent().getSerializableExtra(ObjectTranDemo.SER_KEY);
- mTextView.setText("You name is: " + mPerson.getName() + "/n"+
- "You age is: " + mPerson.getAge());
- setContentView(mTextView);
- }
- }
IntentDemo2.java
- package cn.caiwb.intent;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class IntentDemo2 extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TextView mTextView = new TextView(this);
- Book mBook = (Book)getIntent().getParcelableExtra(ObjectTranDemo.PAR_KEY);
- mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+
- "Author is: " + mBook.getAuthor() + "/n" +
- "PublishTime is: " + mBook.getPublishTime());
- setContentView(mTextView);
- }
- }
OK 可以了~
转自:链接
Intent传递对象的两种方法(Serializable,Parcelable) (转)的更多相关文章
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...
- [转]Android中Intent传递对象的两种方法(Serializable,Parcelable)
http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种 ...
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
[转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object); ...
- Intent传递对象的两种方法
Android为intent提供了两种传递对象参数类型的方法 分别需要使实体类实现Serializable接口.Parcelable接口 首先我们要知道,传递对象,需要先将对象序列化 一.那么为什么要 ...
- Intent传递对象的几种方式
原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...
- 读取xml文件转成List<T>对象的两种方法(附源码)
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...
- 取xml文件转成List<T>对象的两种方法
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...
- 在Delphi中使用C++对象(两种方法,但都要改造C++提供的DLL)
Delphi是市场上最好的RAD工具,但是现在C++占据着主导地位,有时针对一个问题很难找到Delphi或Pascal的解决方案.可是却可能找到了一个相关的C++类.本文描述几种在Delphi代码中使 ...
- WCF生成客户端代理对象的两种方法的解释
最近在封装WCF,有一些很好的实践就记录下来,大家可以放心使用,所有代码都已经调试过.如果有高手可以大家探讨一下. 在WCF中有两种不同的方法可以用于创建客户端服务对象,他们分别为: 1. 代理构造法 ...
随机推荐
- Oracl各个版本的下载地址
http://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/112010-win32soft-098630-z ...
- sharepoint2010匿名访问
怎样在SharePoint 2010网站中启用匿名访问 SharePoint 2010的改动比较大,尤其是相对SharePoint Portal Server 2003来说.本文介绍在SharePoi ...
- POJ 2421(prim)
http://poj.org/problem?id=2421 这个题和poj1258是一样的,只要在1258的基础上那么几行代码,就可以A,水. 题意:还是n连通问题,和1258不同的就是这个还有几条 ...
- linux shell 中 printf 与 echo的区别
echo echo是非常常用的shell命令.参数如下: -e:打开反斜杠字符backslash-escaped的解析,即对/n,/t等字符进行解析,而不视之为两个字符 -E:关闭反斜杠字符 ...
- python之基本数据类型
Python运算符及基本数据类型 运算符: 1.算数运算 2. 比较运算 3. 赋值运算 4. 逻辑运算 5. 成员运算 基本数据类型: 1. 数字 int(整型) 在32位机器上,整数的位数为32位 ...
- nyoj412_bitset_
Same binary weight 时间限制:300 ms | 内存限制:65535 KB 难度:3 描述 The binary weight of a positive integer ...
- linux 解压缩
tar f 使用档案名字,这个参数是最后一个参数,后面只能接档案名 c 建立压缩档案 x 解压 t 查看内容 r 向压缩归档文件末尾追加文件 u 更新原压缩包中的文件 z 有gzip属性的 j 有bz ...
- 【leetcode】Combinations (middle)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [Android Pro] Android保存图片到系统图库
http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/ http://blog.csdn.net/x ...
- Hyper snap
图像->分辨率,设置成300dpi,一般论文的分辨率要求.