原文连接:http://blog.csdn.net/Android_Tutor/archive/2010/07/16/5740845.aspx

使用intent可以在两个Acitivity之间传递数据,可以是int,string 数组,list等等。

但是有时候要传递一个对象,那怎么办呢

网上找了资料,并且运行了一下

mian.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="@string/hello"
/>
<Button android:id="@+id/btn_01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Serilizable"/>
<Button android:id="@+id/btn_02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Parcelable"/>
</LinearLayout>

MianActivity.java

package cn.edu.wtu;

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{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_01 = (Button) findViewById(R.id.btn_01);
Button btn_02 = (Button) findViewById(R.id.btn_02);
btn_01.setOnClickListener(this);
btn_02.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_01:{

Intent intent = new Intent(this,PersonView.class);
Person mPerson = new Person();
mPerson.setAge(20);
mPerson.setName("moon");
Bundle bundle = new Bundle();
bundle.putSerializable("person", mPerson);
intent.putExtras(bundle);
startActivity(intent);
break;
}

case R.id.btn_02:{
Intent intent = new Intent(this,BookView.class);
Book book = new Book();
book.setName("manmonth");
book.setTime("1975");
book.setAuthor("Brooks");
Bundle bundle = new Bundle();
bundle.putParcelable("book", book);
intent.putExtras(bundle);
startActivity(intent);
break;
}

}
}
}

Book.java:

package cn.edu.wtu;

import android.os.Parcel;
import android.os.Parcelable;

public class Book implements Parcelable{

private String name;
private String author;
private String time;

public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>(){

@Override
public Book createFromParcel(Parcel source) {
// TODO Auto-generated method stub
Book mBook = new Book();
mBook.name = source.readString();
mBook.time = source.readString();
mBook.author = source.readString();
return mBook;
}

@Override
public Book[] newArray(int size) {
// TODO Auto-generated method stub
return new Book[size];
}

};

@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeString(author);
dest.writeString(time);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

}

Person.java:

package cn.edu.wtu;

import java.io.Serializable;

public class Person implements Serializable{

private static final long serialVersionUID = 1L;

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;
}
}

PersonView:

package cn.edu.wtu;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class PersonView extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
Person person = (Person)getIntent().getSerializableExtra("person");
text.setText("name:"+person.getName()+"/nage:"+person.getAge()+"/n");
setContentView(text);
}
}

BookView:

package cn.edu.wtu;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class BookView extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
Book book = (Book) getIntent().getParcelableExtra("book");
text.setText("name:"+book.getName()+"/nautor:"+book.getAuthor()+"/ntime:"+book.getTime());
setContentView(text);
}

}

更多相关文章

  1. Bundle使用心得
  2. Android(安卓)第三方类库简单使用之EventBus
  3. Android(安卓)OpenGL教程-第六课
  4. 《Android(安卓)JNI》04 对java传进来的数组进行操作并返回
  5. Android(安卓)利用Handle 切换的主线程更新UI
  6. Android-NDK开发之基础--Android(安卓)JNI有关Java类命名方式
  7. android fragment与activity接口传值
  8. Button的高宽无故变大了!
  9. Android(安卓)Intent传递 List

随机推荐

  1. 小型的编程项目有哪些值得推荐?这本神书写
  2. 聊聊 print 的前世今生
  3. java基础知识
  4. Java常用的时间工具类DateTimeUtils.java
  5. 手把手带你爬天猫,获取杜蕾斯评论数据
  6. Oracle19c_cdb数据库自动安装部署脚本
  7. 当谈论迭代器时,我谈些什么?
  8. Python 之父撰文回忆:为什么要创造 pgen
  9. 从 Python 之父的对话聊起,关于知识产权、
  10. Python 为了提升性能,竟运用了共享经济