【转载】作为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; -
-
@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()); -
} -
- }