不同的activity传递自定义对象
来源:互联网 发布:eamic软件下载 编辑:程序博客网 时间:2024/05/21 14:45
有很多时候都需要在不同的Activity之间传递数据。
实现方法有很多,可以用Bundle来传递,也可以用给Intent用putExtra传递附加参数。
当然也可以传递自定义类的对象。为了让自定义类可以在Activity之间传递,必须符合一些条件。常用的方法是自定义类实现Parcelable接口或Serializable接口。Android中建议实现Parcelable。
下面例子中两种方法都有例子。
传递Parcelable过去,再传递Serializable回来。
WeightParcelable.java
- import android.os.Parcel;
- import android.os.Parcelable;
- public class WeightParcelable implements Parcelable {
- private String sex;
- private double height;
- //必须的空构造器,因为下面有一个私有的构造器,否则不能new对象
- public WeightParcelable() {
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public double getHeight() {
- return height;
- }
- public void setHeight(double height) {
- this.height = height;
- }
- @Override
- public int describeContents() {
- return 0;
- }
- //需要覆盖的方法
- @Override
- public void writeToParcel(Parcel out, int flags) {
- out.writeString(sex);
- out.writeDouble(height);
- }
- //关键的事情
- public static final Parcelable.Creator<WeightParcelable> CREATOR = new Parcelable.Creator<WeightParcelable>() {
- public WeightParcelable createFromParcel(Parcel in) {
- return new WeightParcelable(in);
- }
- public WeightParcelable[] newArray(int size) {
- return new WeightParcelable[size];
- }
- };
- //
- private WeightParcelable(Parcel in) {
- sex = in.readString();
- height = in.readDouble();
- }
- }
HeightSerializable.java
- import java.io.Serializable;
- public class HeightSerializable implements Serializable {
- private static final long serialVersionUID = 8502706820090766507L;
- private String sex;
- private double weight;
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public double getWeight() {
- return weight;
- }
- public void setWeight(double weight) {
- this.weight = weight;
- }
- }
EX03_10.java
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.TextView;
- public class EX03_10 extends Activity {
- /** Called when the activity is first created. */
- public static final String parcelableKey = "irdc.ex03_10.parcelableKey";
- public static final String seralizableKey = "irdc.ex03_10.seralizableKey";
- private static final String TAG = "EX03_10";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button b1 = (Button) findViewById(R.id.button1);
- b1.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- EditText et = (EditText) findViewById(R.id.height);
- double height = Double.parseDouble(et.getText().toString());
- String sex = "";
- RadioButton rb1 = (RadioButton) findViewById(R.id.sex1);
- if (rb1.isChecked()) {
- sex = "M";
- } else {
- sex = "F";
- }
- Intent intent = new Intent();
- intent.setClass(EX03_10.this, EX03_10_1.class);
- Bundle bundle = new Bundle();
- WeightParcelable wp = new WeightParcelable();
- wp.setSex(sex);
- wp.setHeight((int) height);
- bundle.putParcelable(parcelableKey, wp);
- intent.putExtras(bundle);
- //注意这里
- startActivityForResult(intent, 1);
- }
- });
- }
- //这个是关键
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- Log.d(TAG, "onActivityResult requestCode=" + requestCode
- + " resultCode= " + resultCode + " data == null["
- + (data == null) + "]");
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == 1) {
- if (resultCode == RESULT_OK) {
- try {
- HeightSerializable hs = (HeightSerializable) data
- .getSerializableExtra(seralizableKey);
- TextView textView4 = (TextView) findViewById(R.id.text4);
- textView4.setText("性别:" + hs.getSex() + "/n标准体重:"
- + hs.getWeight());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
EX03_10_1.java
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TextView;
- public class EX03_10_1 extends Activity {
- private static final String TAG = "EX03_10_1";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.myalyout);
- WeightParcelable wp = (WeightParcelable) getIntent()
- .getParcelableExtra(EX03_10.parcelableKey);
- String sex = wp.getSex();
- double height = wp.getHeight();
- String sexText = "";
- if (sex.equals("M")) {
- sexText = "男性";
- } else {
- sexText = "女性";
- }
- String weight = this.getWeight(sex, height);
- TextView tv1 = (TextView) findViewById(R.id.text1);
- tv1.setText("性别:" + sexText + "/n身高:" + height + "厘米/n标准体重: " + weight
- + "公斤");
- // 返回对象
- Intent data = new Intent();
- HeightSerializable hs = new HeightSerializable();
- hs.setSex(sexText);
- hs.setWeight(Double.valueOf(weight));
- data.putExtra(EX03_10.seralizableKey, hs);
- //从这里返回对象
- setResult(RESULT_OK, data);
- }
- private String format(double num) {
- NumberFormat formatter = new DecimalFormat("0.00");
- String s = formatter.format(num);
- return s;
- }
- private String getWeight(String sex, double height) {
- String weight = "";
- if (sex.equals("M")) {
- weight = format((height - 80) * 0.7);
- } else {
- weight = format((height - 70) * 0.6);
- }
- return weight;
- }
- }
- 不同的activity传递自定义对象
- Android中不同Activity之间传递自定义对象的参数问题的解决
- 不同Activity之间的数据传递---Bundle对象的实现
- Android Activity传递自定义对象
- Android Activity间传递自定义类的对象
- Android Activity间传递自定义类的对象
- Android Activity间传递自定义类的对象
- 两个activity传递list list 自定义对象
- Android中Activity之间传递自定义对象
- Activity间传递自定义类型对象
- Activity之间的 对象传递
- Activity之间的 对象传递
- Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用!
- Ex3.10 不同Activity之间的数据传递——Bundle对象的实现
- Android中级教程之(五)----->不同Activity之间的数据传递,Bundle对象的使用!
- Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用!
- Android笔记三.不同Activity之间的传递数据(Bundle对象的使用)
- 不同Activity之间的数据传递
- ASP.NET页面生命周期的整体把握
- Eclipse 快捷键大全
- SuperWebSocket发布了第一个测试版
- web.config配置文件设置Session的过期时间
- ArcGIS Server ADF开发:根据图层不同属性用不同图标定位兴趣点
- 不同的activity传递自定义对象
- TRUNCATE和DELETE有以下几点区别
- ERROR:Android requires .class compatibility set to 5.0. Please fix project properties.
- 正则的使用展示 (整体贴)
- MSSQL 2000 脚本
- From an architect to a programmer
- ArcGIS Server ADF开发:直线段缓冲分析(生成10公里缓冲区)
- 累是累的来
- 我的Java学习推荐书目