您的位置:首页 > 移动开发 > Android开发

android中自定义数据类型在两个activity间的传递

2011-12-02 18:18 561 查看
作为android开发者大家都知道两个activity之间的跳转及数据的传递是通过intent和bundle来实现,在intent下有挺多方法来协助我们实现连个activity间的交互,但有时我们需要传递的不单单只是一个简单的数据类型,而是我们自己封转的数据对象,二进制对象,那我们改如何实现呢?

要实现它,我们有两个方法,都是去实现android里的接口,他们分别是serialiable和Parcelabel,对于serialable的实现方式比较简单,只需在我们的数据类实现它,并在activity下通过bundle的协助,使用putserialableExtras将对象存放在bundle中,而对于Parcelable我需要重新实现它下面的creator对象,之后通过intent进行传递。

下面是实现这两种方法的代码:

(1)Serialable

package cn.com.wd;

import java.io.Serializable;

public class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String age;
private String sex;
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}

}


package cn.com.wd;

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;
import android.widget.EditText;

public class ObjectPassDemoActivity extends Activity {
private EditText name,id,sex,age;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name = (EditText)findViewById(R.id.editname);
id = (EditText)findViewById(R.id.editage);
sex = (EditText)findViewById(R.id.editsex);
age = (EditText)findViewById(R.id.editage);
button = (Button)findViewById(R.id.next);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String namestr = name.getText().toString();
String idstr = id.getText().toString();
String sexstr = sex.getText().toString();
String agestr = age.getText().toString();
Person person = new Person();
person.setAge(agestr);
person.setId(idstr);
person.setName(namestr);
person.setSex(sexstr);
Bundle bundle = new Bundle();
bundle.putSerializable("personObject", person);
Intent intent = new Intent(ObjectPassDemoActivity.this, ResultActivty.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}


package cn.com.wd;

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

public class ResultActivty extends Activity {
private TextView name,age,id,sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
name = (TextView)findViewById(R.id.name);
age = (TextView)findViewById(R.id.age);
id = (TextView)findViewById(R.id.id);
sex = (TextView)findViewById(R.id.sex);
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
Person person = (Person)bundle.getSerializable("personObject");
name.setText(person.getName());
age.setText(person.getAge());
id.setText(person.getId());
sex.setText(person.getSex());
}

}


(2)Parcelable

package cn.com.PracelAbleTest;

import java.util.HashMap;

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

public class Person implements Parcelable {
public HashMap<String, Object> map = new HashMap<String, Object>();
public String name;

@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.writeMap(map);
dest.writeString(name);
}

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

@Override
public Person createFromParcel(Parcel source) {
// TODO Auto-generated method stub
Person p = new Person();
p.map = source.readHashMap(HashMap.class.getClassLoader());
p.name = source.readString();
return p;
}

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


package cn.com.PracelAbleTest;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

public class ParcelableDemoActivity extends Activity {
private Bitmap bitmap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Intent intent = new Intent();
Person p = new Person();
p.map = new HashMap<String, Object>();
p.map.put("key","ido");
p.map.put("img", bitmap);
p.name = "ok";
intent.putExtra("key",p);
intent.setClass(ParcelableDemoActivity.this,NextDemo.class);
startActivity(intent);
}
}


package cn.com.PracelAbleTest;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

public class NextDemo extends Activity{
ImageView iv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)findViewById(R.id.image);
Intent i = getIntent();
Person p = i.getParcelableExtra("key");
System.out.println("----->"+p.name);
System.out.println("----->"+p.map.get("key"));
Bitmap bitmap = (Bitmap) p.map.get("img");
iv.setImageBitmap(bitmap);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: