不同的activity传递自定义对象

来源:互联网 发布:eamic软件下载 编辑:程序博客网 时间:2024/05/21 14:45

有很多时候都需要在不同的Activity之间传递数据。
实现方法有很多,可以用Bundle来传递,也可以用给Intent用putExtra传递附加参数。
当然也可以传递自定义类的对象。为了让自定义类可以在Activity之间传递,必须符合一些条件。常用的方法是自定义类实现Parcelable接口或Serializable接口。Android中建议实现Parcelable。
下面例子中两种方法都有例子。
传递Parcelable过去,再传递Serializable回来。





WeightParcelable.java

Ruby代码 复制代码
  1. import android.os.Parcel;   
  2. import android.os.Parcelable;   
  3.   
  4. public class WeightParcelable implements Parcelable {   
  5.     private String sex;   
  6.     private double height;   
  7. //必须的空构造器,因为下面有一个私有的构造器,否则不能new对象   
  8.     public WeightParcelable() {   
  9.   
  10.     }   
  11.   
  12.     public String getSex() {   
  13.         return sex;   
  14.     }   
  15.   
  16.     public void setSex(String sex) {   
  17.         this.sex = sex;   
  18.     }   
  19.   
  20.     public double getHeight() {   
  21.         return height;   
  22.     }   
  23.   
  24.     public void setHeight(double height) {   
  25.         this.height = height;   
  26.     }   
  27.   
  28.     @Override  
  29.     public int describeContents() {   
  30.         return 0;   
  31.     }   
  32. //需要覆盖的方法   
  33.     @Override  
  34.     public void writeToParcel(Parcel out, int flags) {   
  35.         out.writeString(sex);   
  36.         out.writeDouble(height);   
  37.     }   
  38. //关键的事情   
  39.     public static final Parcelable.Creator<WeightParcelable> CREATOR = new Parcelable.Creator<WeightParcelable>() {   
  40.         public WeightParcelable createFromParcel(Parcel in) {   
  41.             return new WeightParcelable(in);   
  42.         }   
  43.   
  44.         public WeightParcelable[] newArray(int size) {   
  45.             return new WeightParcelable[size];   
  46.         }   
  47.     };   
  48. //   
  49.     private WeightParcelable(Parcel in) {   
  50.         sex = in.readString();   
  51.         height = in.readDouble();   
  52.     }   
  53. }  



HeightSerializable.java

Java代码 复制代码
  1. import java.io.Serializable;   
  2.   
  3. public class HeightSerializable implements Serializable {   
  4.     private static final long serialVersionUID = 8502706820090766507L;   
  5.     private String sex;   
  6.     private double weight;   
  7.   
  8.     public String getSex() {   
  9.         return sex;   
  10.     }   
  11.   
  12.     public void setSex(String sex) {   
  13.         this.sex = sex;   
  14.     }   
  15.   
  16.     public double getWeight() {   
  17.         return weight;   
  18.     }   
  19.   
  20.     public void setWeight(double weight) {   
  21.         this.weight = weight;   
  22.     }   
  23.   
  24. }  



EX03_10.java

Java代码 复制代码
  1. import android.app.Activity;   
  2. import android.content.Intent;   
  3. import android.os.Bundle;   
  4. import android.util.Log;   
  5. import android.view.View;   
  6. import android.widget.Button;   
  7. import android.widget.EditText;   
  8. import android.widget.RadioButton;   
  9. import android.widget.TextView;   
  10.   
  11. public class EX03_10 extends Activity {   
  12.     /** Called when the activity is first created. */  
  13.     public static final String parcelableKey = "irdc.ex03_10.parcelableKey";   
  14.     public static final String seralizableKey = "irdc.ex03_10.seralizableKey";   
  15.     private static final String TAG = "EX03_10";   
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {   
  19.         super.onCreate(savedInstanceState);   
  20.         setContentView(R.layout.main);   
  21.   
  22.         Button b1 = (Button) findViewById(R.id.button1);   
  23.         b1.setOnClickListener(new Button.OnClickListener() {   
  24.             public void onClick(View v) {   
  25.                 EditText et = (EditText) findViewById(R.id.height);   
  26.                 double height = Double.parseDouble(et.getText().toString());   
  27.                 String sex = "";   
  28.                 RadioButton rb1 = (RadioButton) findViewById(R.id.sex1);   
  29.                 if (rb1.isChecked()) {   
  30.                     sex = "M";   
  31.                 } else {   
  32.                     sex = "F";   
  33.                 }   
  34.                 Intent intent = new Intent();   
  35.                 intent.setClass(EX03_10.this, EX03_10_1.class);   
  36.   
  37.                 Bundle bundle = new Bundle();   
  38.   
  39.                 WeightParcelable wp = new WeightParcelable();   
  40.                 wp.setSex(sex);   
  41.                 wp.setHeight((int) height);   
  42.   
  43.                 bundle.putParcelable(parcelableKey, wp);   
  44.                 intent.putExtras(bundle);   
  45. //注意这里   
  46.                 startActivityForResult(intent, 1);   
  47.             }   
  48.         });   
  49.     }   
  50. //这个是关键   
  51.     @Override  
  52.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  53.         Log.d(TAG, "onActivityResult requestCode=" + requestCode   
  54.                 + " resultCode= " + resultCode + " data == null["  
  55.                 + (data == null) + "]");   
  56.   
  57.         super.onActivityResult(requestCode, resultCode, data);   
  58.         if (requestCode == 1) {   
  59.             if (resultCode == RESULT_OK) {   
  60.                 try {   
  61.                     HeightSerializable hs = (HeightSerializable) data   
  62.                             .getSerializableExtra(seralizableKey);   
  63.   
  64.                     TextView textView4 = (TextView) findViewById(R.id.text4);   
  65.                     textView4.setText("性别:" + hs.getSex() + "/n标准体重:"  
  66.                             + hs.getWeight());   
  67.   
  68.                 } catch (Exception e) {   
  69.                     e.printStackTrace();   
  70.                 }   
  71.             }   
  72.         }   
  73.     }   
  74.   
  75. }  



EX03_10_1.java

Java代码 复制代码
  1. import java.text.DecimalFormat;   
  2. import java.text.NumberFormat;   
  3.   
  4. import android.app.Activity;   
  5. import android.content.Intent;   
  6. import android.os.Bundle;   
  7. import android.util.Log;   
  8. import android.widget.TextView;   
  9.   
  10. public class EX03_10_1 extends Activity {   
  11.     private static final String TAG = "EX03_10_1";   
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.myalyout);   
  17.   
  18.         WeightParcelable wp = (WeightParcelable) getIntent()   
  19.                 .getParcelableExtra(EX03_10.parcelableKey);   
  20.   
  21.         String sex = wp.getSex();   
  22.         double height = wp.getHeight();   
  23.   
  24.         String sexText = "";   
  25.         if (sex.equals("M")) {   
  26.             sexText = "男性";   
  27.         } else {   
  28.             sexText = "女性";   
  29.         }   
  30.   
  31.         String weight = this.getWeight(sex, height);   
  32.   
  33.         TextView tv1 = (TextView) findViewById(R.id.text1);   
  34.         tv1.setText("性别:" + sexText + "/n身高:" + height + "厘米/n标准体重: " + weight   
  35.                 + "公斤");   
  36.   
  37.         // 返回对象   
  38.         Intent data = new Intent();   
  39.         HeightSerializable hs = new HeightSerializable();   
  40.         hs.setSex(sexText);   
  41.         hs.setWeight(Double.valueOf(weight));   
  42.   
  43.         data.putExtra(EX03_10.seralizableKey, hs);   
  44. //从这里返回对象   
  45.         setResult(RESULT_OK, data);   
  46.     }   
  47.   
  48.     private String format(double num) {   
  49.         NumberFormat formatter = new DecimalFormat("0.00");   
  50.         String s = formatter.format(num);   
  51.         return s;   
  52.     }   
  53.   
  54.     private String getWeight(String sex, double height) {   
  55.         String weight = "";   
  56.         if (sex.equals("M")) {   
  57.             weight = format((height - 80) * 0.7);   
  58.         } else {   
  59.             weight = format((height - 70) * 0.6);   
  60.         }   
  61.         return weight;   
  62.     }   
  63. }  
原创粉丝点击