Android Intent传递对象

原文连接: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);
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值