Activity传递对象的方法

Android中通过Intent传递对象类型的方法有两种,一种是Bundle.putSerializable(Key,Object),另一种是Bundle.putParcelable(Key,Object).传递这些对象要满足一定的条件,前者是实现Serializable接口,后一种是实现了Parcelable.

本文的例子是Parcelable接口的方法。

Parcelable需要实现三个函数:writeToParcel、describeContents和CREATOR.

wroteToParcel(Parcel dest,int flags)将需要数列化存储的数据写入外部提供的Parcel对象dest.

describeContent()描述值类型,直接返回0即可

static final Parcelable.Creator 对象 CREATOR:这个CREATOR命名是固定的,而它对应的接口有两个方法必须实现,createFromPrcel(Parcel source)实现从source创建出JavaBean实例的功能;newArray(int size)创建该类长度的数组。

实现Parcelable接口,代码如下:

package com.activity.transfer;
import android.os.Parcel;
import android.os.Parcelable;
public class ParcelableTest implements Parcelable
{
 private String string1;
 private String string2;
 @Override
 public int describeContents() {
  // TODO Auto-generated method stub
  return 0;
 }
 @Override
 public void writeToParcel(Parcel arg0, int arg1) {
  // TODO Auto-generated method stub
  arg0.writeString(string1);
  arg0.writeString(string2);
 }
 
 public void setString1Value(String str1)
 {
  this.string1 = str1;
 }
 public String getString1Value()
 {
  
  return this.string1;
 }
 
 public void setString2Value(String str2)
 {
  this.string2 = str2;
 }
 public String getString2Value()
 {
  
  return this.string2;
 }
 public static final Parcelable.Creator<ParcelableTest> CREATOR = new Creator<ParcelableTest>()
 {
  @Override
  public ParcelableTest createFromParcel(Parcel source) {
   // TODO Auto-generated method stub
   ParcelableTest p = new ParcelableTest();
   p.string1 = source.readString();
   p.string2 = source.readString();
   return p;
  }
  @Override
  public ParcelableTest[] newArray(int size) {
   // TODO Auto-generated method stub
   return new ParcelableTest[size];
  }
  
  
 };
 
}

在mainActivity中创建一个按钮,点击按钮后,将构造对象传递到activity2中,在activity中取值并显示出来。

MainActivity.java代码如下:

 package com.activity.transfer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
 Button button1 = null;
 Button button2 = null;
 EditText edit = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  button1 = (Button)findViewById(R.id.button1);
  button2 = (Button)findViewById(R.id.button2);
  edit = (EditText)findViewById(R.id.editText1);
  button1.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(MainActivity.this,activity2.class);
    String name = edit.getText().toString();
    intent.putExtra("editText", name);
    
    //创建对象
    ParcelableTest mTest = new ParcelableTest();
    mTest.setString1Value(name);
    mTest.setString2Value("string2");
    //绑定数据到bundle
    Bundle mBundle = new Bundle();
    mBundle.putParcelable("DATA", mTest);
    intent.putExtras(mBundle);
    startActivity(intent);
   }
  });
  button2.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    
   }
  });
  
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
}

在activity2.java中显示出来,代码如下:

 package com.activity.transfer;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class activity2 extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity2);
  Intent intent = this.getIntent();
  String name = intent.getStringExtra("editText");
  TextView text = (TextView)findViewById(R.id.textView2);
  //text.setText(name);
  ParcelableTest mTest = (ParcelableTest)getIntent().getParcelableExtra("DATA");
  text.setText(name+"ParcelableTest传过来的值为:"+"string1是:"+mTest.getString1Value()+"string2是:"+mTest.getString2Value());
 }
 
}

整个工程地址:

http://www.eoeandroid.com/thread-538356-1-1.html

时间: 2024-08-30 04:02:02

Activity传递对象的方法的相关文章

页面中传递对象的方法

1.QuerySting在页面间传递值 这种方法的写法:在要传递值的页面,Response.Redirect(url),值包含在在url中.接收值得页面,Request.QueryString["变量名"]. 这是使用起来很简单的一种方式,但是它不是很安全,因为值会在浏览器里的地址栏里显示.并且它也不能传递对象,对长度也有限制,如果要传递的值很多,且对安全要求也高的话,这种方式就不适合了.2.Session变量 我们通常在一个页面中,将值放到session变量中,在另外几个页面使用它.

Intent 传递对象的方法方式

1 一.Serializable 方式 2 这是最简单的一种方法,因为我们要做的就是让我们自定义的对象实现 Serializable 这个空接口. 3 public class Person implements Serializable{ 4 private String mName; 5 private String mAddress; 6 7 public String getName() { 8 return mName; 9 } 10 11 public void setName(St

Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]

http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSerializable(Key,Object);也可以考虑采用Bundle.putParcelable(Key, Object);其中前面一种方法中的Object要实现Serializable接口,后面一种方法中的Object要实现Parcelable接口.下面我们以一个完整的例子来说明. 1.新建一个A

Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!

[转][原文] 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大家更容易理解我还是照常写了一个简单的Demo,大家就一步一步跟我来吧! 第一步:新建一个andr

Android中Intent在Activity之间传递对象[Serializable或Parcelable]

使用intent启动activity /** * Serializeable传递对象的方法 */ private void SerializeMethod(){ Person mPerson = new Person(); mPerson.setName("andy"); mPerson.setAge(26); Intent mIntent = new Intent(this,SerializableDemo.class); Bundle mBundle = new Bundle();

Android中Intent传递对象的两种方法(Serializable,Parcelable)

今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大 家更容易理解我还是照常写了一个简单的Demo,大家就一步一步跟我来吧! 第一步:新建一个Android工程命名为Object

(六十四)Android中Intent传递对象的两种方法(Serializable,Parcelable)

转载自:http://blog.csdn.net/android_tutor/article/details/5740845 大家好,好久不见,今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable

[转]Android中Intent传递对象的两种方法(Serializable,Parcelable)

http://blog.csdn.net/xyz_lmn/article/details/5908355 今天要给大家讲一下Android中Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口,为了让大家更容易理解我还是照常写

Android之Activity之间传递对象

在很多时候,我们需要在Activity之间传递对象,比如当你点击了某列表的item,需要传递给下一个Activity该对象,那我们需要该怎么做呢? Android支持两种传递对象的方式,一种是bundle.putSerializable方式,一种是bundle.putParcelable. 那么下面我们就用一个例子来实践Activity传递对象: 1.首先建立两个类,一个Teacher类表示老师,一个Student类表示学生.内容分别如下: <span style="font-size:1